home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / cpphtp2.jar / chpt_09.gml < prev    next >
Text File  |  1998-03-03  |  109KB  |  2,502 lines

  1. <html>
  2. <chapter>
  3. <section type=Popup name=Terminology title="Terminology">
  4. <page>
  5. <font size=14>
  6. A<br>
  7. abstraction 
  8. <a href="%s1p6"><img src=iicons/bullbib.gif></a>
  9. <br>
  10. ambiguity problem 
  11. <a href="%s15p0"><img src=iicons/bullbib.gif></a>
  12.  
  13. <a href="%s15p4"><img src=iicons/bullbib.gif></a>
  14. <br>
  15. association 
  16. <a href="%s13p1"><img src=iicons/bullbib.gif></a>
  17. <br>
  18. B<br>
  19. base class 
  20. <a href="%s1p2"><img src=iicons/bullbib.gif></a>
  21. <a href="%s1p2"><img src=iicons/bullbib.gif></a>
  22. <a href="%s2p0"><img src=iicons/bullbib.gif></a>
  23. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  24.  
  25. <a href="%s3p0"><img src=iicons/bullbib.gif></a>
  26. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  27. <br>
  28. base-class constructor 
  29. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  30. <br>
  31. base-class initializer 
  32. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  33. <br>
  34. base-class pointer 
  35. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  36. <a href="%s10p3"><img src=iicons/bullbib.gif></a>
  37.  
  38. <a href="%s10p4"><img src=iicons/bullbib.gif></a>
  39. <br>
  40. C<br>
  41. class hierarchy 
  42. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  43. <br>
  44. </font>
  45.  
  46. </page>
  47. <page>
  48. <font size=14>
  49. class libraries 
  50. <a href="%s1p9"><img src=iicons/bullbib.gif></a>
  51. <a href="%s11p1"><img src=iicons/bullbib.gif></a>
  52. <br>
  53. composition 
  54. <a href="%s1p4"><img src=iicons/bullbib.gif></a>
  55. <a href="%s12p0"><img src=iicons/bullbib.gif></a>
  56. <a href="%s13p0"><img src=iicons/bullbib.gif></a>
  57. <br>
  58. customize software 
  59. <a href="%s11p0"><img src=iicons/bullbib.gif></a>
  60. <br>
  61. D<br>
  62. derived class 
  63. <a href="%s1p2"><img src=iicons/bullbib.gif></a>
  64. <a href="%s2p0"><img src=iicons/bullbib.gif></a>
  65. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  66.  
  67. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  68. <br>
  69. derived-class constructor 
  70.  
  71. <a href="%s9p1"><img src=iicons/bullbib.gif></a>
  72. <br>
  73. derived-class pointer 
  74. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  75. <br>
  76. direct base class 
  77. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  78. <a href="%s14p5"><img src=iicons/bullbib.gif></a>
  79. <br>
  80. F<br>
  81. <b>friend</b> of a derived class 
  82.  
  83. <a href="%s15p3"><img src=iicons/bullbib.gif></a>
  84. <br>
  85. function overloading 
  86. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  87. <br>
  88. </font>
  89.  
  90. </page>
  91. <page>
  92. <font size=14>
  93. H<br>
  94. "has a" relationship 
  95. <a href="%s1p7"><img src=iicons/bullbib.gif></a>
  96. <br>
  97. hierarchical structure 
  98. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  99. <br>
  100. I<br>
  101. indirect base class 
  102. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  103.  
  104. <a href="%s14p5"><img src=iicons/bullbib.gif></a>
  105. <br>
  106. infinite recursion 
  107. <a href="^Errors::c:s0p3"><img src=iicons/bullbib.gif></a>
  108. <a href="%s6p8"><img src=iicons/bullbib.gif></a>
  109. <br>
  110. inheritance 
  111. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  112. <a href="%s2p0"><img src=iicons/bullbib.gif></a>
  113. <a href="%s4p4"><img src=iicons/bullbib.gif></a>
  114.  
  115. <a href="%s13p0"><img src=iicons/bullbib.gif></a>
  116. <br>
  117. "is a" relationship 
  118. <a href="%s1p7"><img src=iicons/bullbib.gif></a>
  119. <br>
  120. K<br>
  121. knows a 
  122. <a href="%s13p1"><img src=iicons/bullbib.gif></a>
  123. <br>
  124. M<br>
  125. </font>
  126.  
  127. </page>
  128. <page>
  129. <font size=14>
  130. multiple inheritance 
  131. <a href="%s1p2"><img src=iicons/bullbib.gif></a>
  132.  
  133. <a href="%s2p3"><img src=iicons/bullbib.gif></a>
  134. <a href="%s14p5"><img src=iicons/bullbib.gif></a>
  135. <a href="%s15p0"><img src=iicons/bullbib.gif></a>
  136. <a href="%s15p0"><img src=iicons/bullbib.gif></a>
  137. <a href="%s15p5"><img src=iicons/bullbib.gif></a>
  138. <br>
  139. O<br>
  140. object-oriented 
  141. programming (OOP) 
  142. <a href="%s1p5"><img src=iicons/bullbib.gif></a>
  143.  
  144. <a href="%s2p4"><img src=iicons/bullbib.gif></a>
  145. <a href="%s11p1"><img src=iicons/bullbib.gif></a>
  146. <a href="%s14p8"><img src=iicons/bullbib.gif></a>
  147. <br>
  148. overriding 
  149. <a href="%s6p5"><img src=iicons/bullbib.gif></a>
  150. <br>
  151. P<br>
  152. <b>private</b> base class 
  153. <a href="%s7p1"><img src=iicons/bullbib.gif></a>
  154. <br>
  155. <b>private</b> inheritance 
  156. <a href="%s1p4"><img src=iicons/bullbib.gif></a>
  157.  
  158. <a href="%s2p5"><img src=iicons/bullbib.gif></a>
  159. <a href="%s4p5"><img src=iicons/bullbib.gif></a>
  160. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  161. <br>
  162. <b>protected</b> base class 
  163. <a href="%s7p1"><img src=iicons/bullbib.gif></a>
  164. <br>
  165. <b>protected</b> data member 
  166.  
  167. <a href="%s14p1"><img src=iicons/bullbib.gif></a>
  168. <a href="%s14p7"><img src=iicons/bullbib.gif></a>
  169. <br>
  170. </font>
  171.  
  172. </page>
  173. <page>
  174. <font size=14>
  175. <b>protected</b> inheritance 
  176.  
  177. <a href="%s1p4"><img src=iicons/bullbib.gif></a>
  178. <a href="%s2p5"><img src=iicons/bullbib.gif></a>
  179. <br>
  180. <b>public</b> base class 
  181. <a href="%s7p1"><img src=iicons/bullbib.gif></a>
  182. <br>
  183. <b>public</b> inheritance 
  184. <a href="%s1p4"><img src=iicons/bullbib.gif></a>
  185.  
  186. <a href="%s2p5"><img src=iicons/bullbib.gif></a>
  187. <a href="%s10p0"><img src=iicons/bullbib.gif></a>
  188. <a href="%s12p0"><img src=iicons/bullbib.gif></a>
  189. <a href="%s14p2"><img src=iicons/bullbib.gif></a>
  190. <a href="%s14p7"><img src=iicons/bullbib.gif></a>
  191. <br>
  192. <b>public</b> interface 
  193. <a href="%s4p4"><img src=iicons/bullbib.gif></a>
  194. <a href="%s14p2"><img src=iicons/bullbib.gif></a>
  195. <br>
  196. S<br>
  197. single inheritance 
  198. <a href="%s1p2"><img src=iicons/bullbib.gif></a>
  199.  
  200. <a href="%s15p4"><img src=iicons/bullbib.gif></a>
  201. <a href="^Engineer::c:s0p18"><img src=iicons/bullbib.gif></a>
  202. <br>
  203. software reusability 
  204. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  205.  
  206. <a href="%s11p4"><img src=iicons/bullbib.gif></a>
  207. <br>
  208. standardized reusable 
  209. component 
  210. <a href="%s1p9"><img src=iicons/bullbib.gif></a>
  211. <br>
  212. subclass 
  213. <a href="%s2p1"><img src=iicons/bullbib.gif></a>
  214. <br>
  215. </font>
  216.  
  217. </page>
  218. <page>
  219. <font size=14>
  220. superclass 
  221. <a href="%s2p1"><img src=iicons/bullbib.gif></a>
  222. <br>
  223. U<br>
  224. uses a 
  225. <a href="%s13p0"><img src=iicons/bullbib.gif></a>
  226. <br>
  227. <br>
  228. </font>
  229.  
  230. </page>
  231. </section>
  232. <section type=Popup name=Quotes title="Quotes">
  233. <page>
  234. <i>Say not you know another 
  235. entirely, till you have 
  236. divided an</i>  <br>
  237. <i>inheritance with him.</i> <br>
  238. Johann Kasper Lavater<br>
  239. <br>
  240.  
  241. </page>
  242. <page>
  243. <i>This method is to define 
  244. as the number of a class 
  245. the class of all classes 
  246. similar to the given class.</i> <br>
  247. Bertrand Russell<br>
  248. <br>
  249.  
  250. </page>
  251. <page>
  252. <i>A deck of cards was built 
  253. like the purest of 
  254. hierarchies, with every 
  255. card a master to those 
  256. below it, a lackey to those 
  257. above it.</i> <br>
  258. Ely Culbertson<br>
  259. <br>
  260.  
  261. </page>
  262. <page>
  263. <i>Good as it is to inherit a 
  264. library, it is better to 
  265. collect one.</i>  <br>
  266. Augustine Birrell<br>
  267. <br>
  268.  
  269. </page>
  270. <page>
  271. <i>Save base authority from 
  272. others' books.</i> <br>
  273. William Shakespeare, 
  274. Love's Labour's Lost<br>
  275. <br>
  276.  
  277. </page>
  278. </section>
  279. <section type=Popup name=Illustration title="Illustrations">
  280. <page>
  281. <a href="^Illustration::c:s0p1">Fig. 9.1</a>  Some simple inheritance examples.<br>
  282. <a href="^Illustration::c:s0p3">Fig. 9.2</a>  An inheritance hierarchy for university community members.<br>
  283. <a href="^Illustration::c:s0p2">Fig. 9.3</a>  A portion of a <b>Shape</b> class hierarchy.<br>
  284. <a href="^Code::c:s0p0">Fig. 9.4</a>  Casting base-class pointers to derived-class pointers.<br>
  285. <a href="^Code::c:s0p1">Fig. 9.5</a>  Overriding a base-class member function in a derived class.<br>
  286. <a href="^Illustration::c:s0p5">Fig. 9.6</a>  Summary of base-class member accessibility in a derived class.<br>
  287. <a href="^Code::c:s0p2">Fig. 9.7</a>  Order in which base-class and derived-class constructors and destructors are called.<br>
  288. <a href="^Code::c:s0p3">Fig. 9.8</a>  Demonstrating class <b>Point</b>.<br>
  289. <a href="^Code::c:s0p4">Fig. 9.9</a>  Demonstrating class <b>Circle</b>.<br>
  290. <a href="^Code::c:s0p5">Fig. 9.10</a>  Demonstrating class <b>Cylinder</b>.<br>
  291. <a href="^Code::c:s0p6">Fig. 9.11</a>  Demonstrating multiple inheritance.<br>
  292. <br>
  293.  
  294. </page>
  295. <page>
  296. <font size=18><a href="~audio/Ch09/09fig001.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 9.1 - Some simple inheritance examples.<img src="graphics/ch09/fig09001.gif" ></font><br>
  297.  
  298. </page>
  299. <page>
  300. <font size=18><a href="~audio/Ch09/09fig003.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 9.3 - A portion of a <b>Shape</b> class hierarchy.<img src="graphics/ch09/fig09003.gif" ></font><br>
  301.  
  302. </page>
  303. <page>
  304. <font size=18><a href="~audio/Ch09/09fig002.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 9.2 - An inheritance hierarchy for university community members.<img src="graphics/ch09/fig09002.gif" ></font><br>
  305.  
  306. </page>
  307. <page>
  308. <font size=18><a href="~audio/Ch09/09fig006.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 9.6 - Summary of base-class member accessibility in a derived class. (Part 1 
  309. of 2)   </font><br>
  310. <img src="graphics/ch09/fig0906a.gif" ><br>
  311.  
  312. </page>
  313. <page>
  314. <font size=18>Figure 9.6 - Summary of base-class member accessibility in a derived class. (Part 2 
  315. of 2)</font><br>
  316. <img src="graphics/ch09/fig0906b.gif" ><br>
  317.  
  318. </page>
  319. </section>
  320. <section type=Popup name=Practice title="Good Practices">
  321. <page>
  322. Multiple inheritance is 
  323. a powerful capability 
  324. when used properly. 
  325. Multiple inheritance 
  326. should be used when an 
  327. "is a" relationship 
  328. exists between a new 
  329. type and two or more 
  330. existing types (i.e., type <br>
  331.  
  332. </page>
  333. <page>
  334. A "is a" type B and 
  335. type A "is a" type C). <br>
  336. <br>
  337.  
  338. </page>
  339. </section>
  340. <section type=Popup name=Answers title="Answers">
  341. <page pagename="Answer 9.1">
  342. <b>Answer 9.1</b><br>
  343. a)  derived, base.<br>
  344. b) multiple inheritance.<br>
  345. c) software reusability.<br>
  346. d) derived, base.<br>
  347. e) cast.<br>
  348. f) <b>public</b>, <b>protected</b>, <b>private</b>.<br>
  349. g) <b>public</b>, <b>protected</b>.<br>
  350. h) <b>protected</b>, <b>protected</b>.<br>
  351. i) composition, inheritance.<br>
  352. <foreign  name="exercises" url="^Exercises::c:s0p0">
  353. <br>
  354.  
  355. </page>
  356. <page pagename="Answer 9.12">
  357. <b>Answer 9.12</b><br>
  358. CommunityMember<br>
  359.    Employee<br>
  360.       Staff<br>
  361.          Maintenance<br>
  362.          Janitorial<br>
  363.       Faculty<br>
  364.          Administrator<br>
  365.          Professor<br>
  366.             TenuredProfessor<br>
  367.    Student<br>
  368. <foreign  name="exercises" url="^Exercises::c:s0p12">
  369.  
  370. </page>
  371. <page pagename="Answer 9.12">
  372.       Graduate<br>
  373.          MasterCandidate<br>
  374.          DoctoralCandidate<br>
  375.       Undergraduate<br>
  376.          Freshman<br>
  377.          Sophomore<br>
  378.          Junior<br>
  379.          Senior<br>
  380. <foreign  name="exercises" url="^Exercises::c:s0p12">
  381. <br>
  382.  
  383. </page>
  384. <page pagename="Answer 9.10 ">
  385. <b>Answer 9.10 </b><br>
  386. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  387. the file cpphtp2/answers/P9_10.zip to your hard drive and unzip the program 
  388. code.<br>
  389. <foreign  name="exercises" url="^Exercises::c:s0p10">
  390.  
  391. </page>
  392. </section>
  393. <section type=Popup name=Exercises title="Exercises">
  394. <page pagename="Exercise 9.1">
  395. <b>Exercise 9.1</b><br>
  396. Fill in the blanks in each of the following:<br>
  397. a)  If the class <b>Alpha</b> inherits from the class <b>Beta</b>, class <b>Alpha</b> is called the 
  398. ________ class and class <b>Beta</b> is called the ________ class.<br>
  399. b)  C++ provides for ________ which allows a derived class to inherit from 
  400. many base classes, even if these base classes are unrelated.<br>
  401. c)  Inheritance enables ________ which saves time in development, and 
  402. encourages using previously proven and high-quality software.<br>
  403. d)  An object of a ________ class can be treated as an object of its 
  404. corresponding ________ class.<br>
  405. e)  To convert a base-class pointer to a derived class pointer, a ________ must 
  406. be used because the compiler considers this a dangerous operation.<br>
  407. <foreign  name="answers" url="^Answers::c:s0p0">
  408.  
  409. </page>
  410. <page pagename="Exercise 9.1">
  411. f)  The three member access specifiers are ________, ________ and 
  412. ________.<br>
  413. g)  When deriving a class from a base class with <b>public</b> inheritance, <b>public</b> 
  414. members of the base class become ________ members of the derived class, 
  415. and <b>protected</b> members of the base class become ________ members of the 
  416. derived class.<br>
  417. h)  When deriving a class from a base class with <b>protected</b> inheritance, <b>public</b> 
  418. members of the base class become ________ members of the derived class, 
  419. and <b>protected</b> members of the base class become ________ members of the 
  420. derived class.<br>
  421. i)  A "has a" relationship between classes represents ________ and an "is a" 
  422. relationship between classes represents _______ .<br>
  423. <foreign  name="answers" url="^Answers::c:s0p0">
  424.  
  425. </page>
  426. <page pagename="Exercise 9.2">
  427. <b>Exercise 9.2</b><br>
  428. Consider the class <b>Bicycle</b>. Given your knowledge of some common 
  429. components of bicycles, show a class hierarchy in which the class <b>Bicycle</b> 
  430. inherits from other classes, which, in turn, inherit from yet other classes. Discuss 
  431. the instantiation of various objects of class <b>Bicycle</b>. Discuss inheritance from 
  432. class <b>Bicycle</b> for other closely related derived classes.<br>
  433. <br>
  434.  
  435. </page>
  436. <page pagename="Exercise 9.3">
  437. <b>Exercise 9.3</b><br>
  438. Briefly define each of the following terms: inheritance, multiple inheritance, 
  439. base class and derived class.<br>
  440. <br>
  441. <br>
  442.  
  443. </page>
  444. <page pagename="Exercise 9.4">
  445. <b>Exercise 9.4</b><br>
  446. Discuss why converting a base-class pointer to a derived-class pointer is 
  447. considered dangerous by the compiler.<br>
  448. <br>
  449. <br>
  450.  
  451. </page>
  452. <page pagename="Exercise 9.5">
  453. <b>Exercise 9.5</b><br>
  454. Distinguish between single inheritance and multiple inheritance.<br>
  455. <br>
  456. <br>
  457.  
  458. </page>
  459. <page pagename="Exercise 9.6">
  460. <b>Exercise 9.6</b><br>
  461. (True/False) A derived class is often called a subclass because it represents a 
  462. subset of its base class, i.e., a derived class is generally smaller than its base 
  463. class.<br>
  464. <br>
  465. <br>
  466.  
  467. </page>
  468. <page pagename="Exercise 9.7">
  469. <b>Exercise 9.7</b><br>
  470. (True/False) A derived-class object is also an object of that derived class's base 
  471. class.<br>
  472. <br>
  473. <br>
  474.  
  475. </page>
  476. <page pagename="Exercise 9.8">
  477. <b>Exercise 9.8</b><br>
  478. Some programmers prefer not to use <b>protected</b> access because it breaks the 
  479. encapsulation of the base class. Discuss the relative merits of using <b>protected</b> 
  480. access vs. insisting on using <b>private</b> access in base classes.<br>
  481. <br>
  482. <br>
  483.  
  484. </page>
  485. <page pagename="Exercise 9.9">
  486. <b>Exercise 9.9</b><br>
  487. Many programs written with inheritance could be solved with composition 
  488. instead, and vice versa. Discuss the relative merits of these approaches in the 
  489. context of the <b>Point</b>, <b>Circle</b>, <b>Cylinder</b> class hierarchy in this chapter. Rewrite 
  490. the program of <a href="^Code::c:s0p5"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.10</a> (and the supporting classes) to use composition rather 
  491. than inheritance. After you do this, reassess the relative merits of the two 
  492. approaches both for the <b>Point</b>, <b>Circle</b>, <b>Cylinder</b> problem and for object-oriented 
  493. programs in general.<br>
  494. <br>
  495.  
  496. </page>
  497. <page pagename="Exercise 9.10">
  498. <b>Exercise 9.10</b><br>
  499. Rewrite the <b>Point</b>, <b>Circle</b>, <b>Cylinder</b> program of <a href="^Code::c:s0p5"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.10</a> as a <b>Point</b>, <b>Square</b>, 
  500. <b>Cube</b> program. Do this two ways--once with inheritance and once with 
  501. composition.<br>
  502. <foreign  name="answers" url="^Answers::c:s0p3">
  503.  
  504. </page>
  505. <page pagename="Exercise 9.11">
  506. <b>Exercise 9.11</b><br>
  507. In the chapter, we stated, "When a base-class member is inappropriate for a 
  508. derived class, that member can be overridden in the derived class with an 
  509. appropriate implementation." If this is done, does the derived-class-is-a-base-
  510. class-object relationship still hold? Explain your answer.<br>
  511. <br>
  512. <br>
  513.  
  514. </page>
  515. <page pagename="Exercise 9.12">
  516. <b>Exercise 9.12</b><br>
  517. Study the inheritance hierarchy of <a href="^Illustration::c:s0p3"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 9.2</a>. For each class, indicate some 
  518. common attributes and behaviors consistent with the hierarchy. Add some other 
  519. classes (e.g., <b>UndergraduateStudent</b>, <b>GraduateStudent</b>, <b>Freshman</b>, 
  520. <b>Sophomore</b>, <b>Junior</b>, <b>Senior</b>, etc.) to enrich the hierarchy.<br>
  521. <foreign  name="answers" url="^Answers::c:s0p1">
  522. <br>
  523.  
  524. </page>
  525. <page pagename="Exercise 9.13">
  526. <b>Exercise 9.13</b><br>
  527. Write an inheritance hierarchy for class <b>Quadrilateral</b>, <b>Trapezoid</b>, 
  528. <b>Parallelogram</b>, <b>Rectangle</b> and <b>Square</b>. Use <b>Quadrilateral</b> as the base class of 
  529. the hierarchy. Make the hierarchy as deep (i.e., as many levels) as possible. The 
  530. private data of <b>Quadrilateral</b> should be the <b>(x, y)</b> coordinate pairs for the four 
  531. endpoints of the <b>Quadrilateral</b>. Write a driver program that instantiates and 
  532. displays objects of each of these classes.<br>
  533. <br>
  534. <br>
  535.  
  536. </page>
  537. <page pagename="Exercise 9.14">
  538. <b>Exercise 9.14</b><br>
  539. Write down all the shapes you can think of--both two-dimensional and three-
  540. dimensional--and form those shapes into a shape hierarchy. Your hierarchy 
  541. should have base class <b>Shape</b> from which class <b>TwoDimensionalShape</b> and 
  542. class <b>ThreeDimensionalShape</b> are derived. Once you have developed the 
  543. hierarchy, define each of the classes in the hierarchy. We will use this hierarchy 
  544. in the exercises of Chapter 10 to process all shapes as objects of base-class 
  545. <b>Shape</b>. This is a technique called polymorphism.<br>
  546. <br>
  547. <br>
  548.  
  549. </page>
  550. </section>
  551. <section type=Popup name=Perform title="Performance">
  552. <page>
  553. When performance is a 
  554. major concern, 
  555. programmers may want 
  556. to see source code of 
  557. classes they are 
  558. inheriting from so they 
  559. can tune the code to 
  560. meet their performance 
  561. requirements.<br>
  562. <br>
  563.  
  564. </page>
  565. <page>
  566. If classes produced 
  567. through inheritance are 
  568. larger than they need to 
  569. be, memory and 
  570. processing resources 
  571. may be wasted. Inherit 
  572. from the class "closest" 
  573. to what you need.<br>
  574. <br>
  575.  
  576. </page>
  577. </section>
  578. <section type=Popup name=Code title="Code Examples">
  579. <page>
  580. <font size=18>Figure 9.4  Casting base-class pointers to derived-class pointers.</font><br>
  581. <applet code=gsl.win.helper.TextBox width=580 height=300>
  582. <param  name="file" value="code/ch09/fig09_04.txt">
  583. </applet><br>
  584. <br>
  585. <foreign  name="copy2disk" url="!jcpy Source/fig09_04.jar">
  586. <foreign  name="audio" url="~audio/Ch09/09fig004.au">
  587. <foreign  name="execute" url="!jarexe Run/fig09_04.jar">
  588. <br>
  589.  
  590. </page>
  591. <page>
  592. <font size=18>Figure 9.5  Overriding a base-class member function in a derived class.</font><br>
  593. <applet code=gsl.win.helper.TextBox width=580 height=300>
  594. <param  name="file" value="code/ch09/fig09_05.txt">
  595. </applet><br>
  596. <br>
  597. <foreign  name="copy2disk" url="!jcpy Source/fig09_05.jar">
  598. <foreign  name="audio" url="~audio/Ch09/09fig005.au">
  599. <foreign  name="execute" url="!jarexe Run/fig09_05.jar">
  600. <br>
  601.  
  602. </page>
  603. <page>
  604. <font size=18>Figure 9.7  Order in which base-class and derived-class constructors and destructors are called.</font><br>
  605. <applet code=gsl.win.helper.TextBox width=580 height=300>
  606. <param  name="file" value="code/ch09/fig09_07.txt">
  607. </applet><br>
  608. <br>
  609. <foreign  name="copy2disk" url="!jcpy Source/fig09_07.jar">
  610. <foreign  name="audio" url="~audio/Ch09/09fig007.au">
  611. <foreign  name="execute" url="!jarexe Run/fig09_07.jar">
  612. <br>
  613.  
  614. </page>
  615. <page>
  616. <font size=18>Figure 9.8  Demonstrating class <b>Point</b>.</font><br>
  617. <applet code=gsl.win.helper.TextBox width=580 height=300>
  618. <param  name="file" value="code/ch09/fig09_08.txt">
  619. </applet><br>
  620. <br>
  621. <foreign  name="copy2disk" url="!jcpy Source/fig09_08.jar">
  622. <foreign  name="audio" url="~audio/Ch09/09fig008.au">
  623. <foreign  name="execute" url="!jarexe Run/fig09_08.jar">
  624. <br>
  625.  
  626. </page>
  627. <page>
  628. <font size=18>Figure 9.9  Demonstrating class <b>Circle</b>.</font><br>
  629. <applet code=gsl.win.helper.TextBox width=580 height=300>
  630. <param  name="file" value="code/ch09/fig09_09.txt">
  631. </applet><br>
  632. <br>
  633. <foreign  name="copy2disk" url="!jcpy Source/fig09_09.jar">
  634. <foreign  name="audio" url="~audio/Ch09/09fig009.au">
  635. <foreign  name="execute" url="!jarexe Run/fig09_09.jar">
  636. <br>
  637.  
  638. </page>
  639. <page>
  640. <font size=18>Figure 9.10  Demonstrating class <b>Cylinder</b>.</font><br>
  641. <applet code=gsl.win.helper.TextBox width=580 height=300>
  642. <param  name="file" value="code/ch09/fig09_10.txt">
  643. </applet><br>
  644. <br>
  645. <foreign  name="copy2disk" url="!jcpy Source/fig09_10.jar">
  646. <foreign  name="audio" url="~audio/Ch09/09fig010.au">
  647. <foreign  name="execute" url="!jarexe Run/fig09_10.jar">
  648. <br>
  649.  
  650. </page>
  651. <page>
  652. <font size=18>Figure 9.11  Demonstrating multiple inheritance.</font><br>
  653. <applet code=gsl.win.helper.TextBox width=580 height=300>
  654. <param  name="file" value="code/ch09/fig09_11.txt">
  655. </applet><br>
  656. <br>
  657. <foreign  name="copy2disk" url="!jcpy Source/fig09_11.jar">
  658. <foreign  name="audio" url="~audio/Ch09/09fig011.au">
  659. <foreign  name="execute" url="!jarexe Run/fig09_11.jar">
  660. <br>
  661.  
  662. </page>
  663. </section>
  664. <section type=Popup name=Objective title="Objectives">
  665. <page>
  666. <indent width=8 delay>*   To be able to create new classes by inheriting from existing classes.</indent>
  667. <indent width=8 delay>*   To understand how inheritance promotes software reusability.</indent>
  668. <indent width=8 delay>*   To understand the notions of base classes and derived classes.</indent>
  669. <indent width=8 delay>*   To be able to use multiple inheritance to derive a class from several base 
  670. classes.</indent>
  671. <foreign  name="audio" url="~audio/Ch09/09obj.au">
  672.  
  673. </page>
  674. </section>
  675. <section type=Popup name=Errors title="Common Errors">
  676. <page>
  677. Treating a base-class 
  678. object as a derived-
  679. class object can cause 
  680. errors.<br>
  681. <br>
  682.  
  683. </page>
  684. <page>
  685. Explicitly casting a 
  686. base-class pointer that 
  687. points to a base-class 
  688. object into a derived-
  689. class pointer and then 
  690. referring to derived-
  691. class members that do 
  692. not exist in that object 
  693. can lead to run-time 
  694. logic errors.<br>
  695.  
  696. </page>
  697. <page>
  698. When a base-class 
  699. member function is 
  700. overridden in a derived 
  701. class, it is common to 
  702. have the derived-class 
  703. version call the base-
  704. class version and do 
  705. some additional work. 
  706. Not using the scope-
  707. resolution operator to <br>
  708.  
  709. </page>
  710. <page>
  711. reference the base 
  712. class's member 
  713. function causes infinite 
  714. recursion because the 
  715. derived-class member 
  716. function actually calls 
  717. itself. This will 
  718. eventually cause the 
  719. system to exhaust <br>
  720.  
  721. </page>
  722. <page>
  723. memory, a fatal 
  724. execution-time error.<br>
  725. <br>
  726.  
  727. </page>
  728. <page>
  729. Assigning a derived-
  730. class object to an object 
  731. of a corresponding base 
  732. class, then attempting 
  733. to reference derived-
  734. class-only members in 
  735. the new base-class 
  736. object is a syntax error.<br>
  737. <br>
  738.  
  739. </page>
  740. <page>
  741. Casting a base-class 
  742. pointer to a derived-
  743. class pointer can cause 
  744. errors if that pointer is 
  745. then used to reference a 
  746. base-class object that 
  747. does not have the 
  748. desired derived-class 
  749. members.<br>
  750. <br>
  751.  
  752. </page>
  753. </section>
  754. <section type=Popup name=Engineer title="Engineering">
  755. <page>
  756. In general, declare data 
  757. members of a class 
  758. <b>private</b> and use 
  759. <b>protected</b> only as a 
  760. "last resort" when 
  761. systems need to be 
  762. tuned to meet unique 
  763. performance 
  764. requirements.<br>
  765. <br>
  766.  
  767. </page>
  768. <page>
  769. A derived class cannot 
  770. directly access <b>private</b> 
  771. members of its base 
  772. class.<br>
  773. <br>
  774.  
  775. </page>
  776. <page>
  777. In inheritance, base-
  778. class constructors are 
  779. called in the order in 
  780. which inheritance is 
  781. specified in the 
  782. derived-class 
  783. definition. The order in 
  784. which the base-class 
  785. constructors are 
  786. specified in the <br>
  787.  
  788. </page>
  789. <page>
  790. derived-class member 
  791. initializer list does not 
  792. affect the order of 
  793. construction.<br>
  794. <br>
  795.  
  796. </page>
  797. <page>
  798. The order in which 
  799. member objects are 
  800. constructed is the order 
  801. in which those objects 
  802. are declared within the 
  803. class definition. The 
  804. order in which the 
  805. member initializers are 
  806. listed does not affect <br>
  807.  
  808. </page>
  809. <page>
  810. the order of 
  811. construction. <br>
  812. <br>
  813.  
  814. </page>
  815. <page>
  816. Suppose we create an 
  817. object of a derived class 
  818. where both the base 
  819. class and the derived 
  820. class contain objects of 
  821. other classes. When an 
  822. object of that derived 
  823. class is created, first the 
  824. constructors for the 
  825. base class's member <br>
  826.  
  827. </page>
  828. <page>
  829. objects execute, then 
  830. the base-class 
  831. constructor executes, 
  832. then the constructors 
  833. for the derived class's 
  834. member objects 
  835. execute, then the 
  836. derived class's 
  837. constructor executes. 
  838. Destructors are called <br>
  839.  
  840. </page>
  841. <page>
  842. in the reverse of the 
  843. order in which their 
  844. corresponding 
  845. constructors are called.<br>
  846. <br>
  847.  
  848. </page>
  849. <page>
  850. In theory, users do not 
  851. need to see the source 
  852. code of classes from 
  853. which they inherit. In 
  854. practice, people who 
  855. license classes tell us 
  856. that the customers often 
  857. demand the source 
  858. code. Programmers still 
  859. seem reluctant to <br>
  860.  
  861. </page>
  862. <page>
  863. incorporate code into 
  864. their programs when 
  865. this code has been 
  866. written by other people.<br>
  867. <br>
  868.  
  869. </page>
  870. <page>
  871. In an object-oriented 
  872. system, classes are 
  873. often closely related. 
  874. "Factor out" common 
  875. attributes and behaviors 
  876. and place these in a 
  877. base class. Then use 
  878. inheritance to form 
  879. derived classes.<br>
  880. <br>
  881.  
  882. </page>
  883. <page>
  884. Creating a derived class 
  885. does not affect its base 
  886. class's source code or 
  887. object code; the 
  888. integrity of a base class 
  889. is preserved by 
  890. inheritance.<br>
  891. <br>
  892.  
  893. </page>
  894. <page>
  895. Modifications to a base 
  896. class do not require 
  897. derived classes to 
  898. change as long as the 
  899. <b>public</b> and <b>protected</b> 
  900. interfaces to the base 
  901. class remain 
  902. unchanged. Derived 
  903. classes may, however, 
  904. need to be recompiled.<br>
  905.  
  906. </page>
  907. <page>
  908. A derived class 
  909. contains the attributes 
  910. and behaviors of its 
  911. base class. A derived 
  912. class can also contain 
  913. additional attributes 
  914. and behaviors. With 
  915. inheritance, the base 
  916. class can be compiled 
  917. independently of the <br>
  918.  
  919. </page>
  920. <page>
  921. derived class. Only the 
  922. derived class's 
  923. incremental attributes 
  924. and behaviors need to 
  925. be compiled to be able 
  926. to combine these with 
  927. the base class to form 
  928. the derived class. <br>
  929. <br>
  930.  
  931. </page>
  932. <page>
  933. Program modifications 
  934. to a class that is a 
  935. member of another 
  936. class do not require the 
  937. enclosing class to 
  938. change as long as the 
  939. <b>public</b> interface to the 
  940. member class remains 
  941. unchanged. Note that 
  942. the composite class <br>
  943.  
  944. </page>
  945. <page>
  946. may, however, need to 
  947. be recompiled.<br>
  948. <br>
  949.  
  950. </page>
  951. <page>
  952. Multiple inheritance is 
  953. a powerful feature, but 
  954. it can introduce 
  955. complexity into a 
  956. system. Great care is 
  957. required in the design 
  958. of a system to use 
  959. multiple inheritance 
  960. properly; it should not 
  961. be used when single <br>
  962.  
  963. </page>
  964. <page>
  965. inheritance will do the 
  966. job.<br>
  967. <br>
  968.  
  969. </page>
  970. </section>
  971. <section type=Body name=Default title="9 Inheritance">
  972. <page>
  973. <font size=18 bold>9 Inheritance</font><hr>
  974. <a href="#s1p0">9.1<spacer width=20 height=1>Introduction</a>  <br>
  975. <a href="#s2p0">9.2<spacer width=20 height=1>Inheritance: Base Classes and Derived Classes</a>  <br>
  976. <a href="#s3p0">9.3<spacer width=20 height=1>Protected Members</a>  <br>
  977. <a href="#s4p0">9.4<spacer width=20 height=1>Casting Base-Class Pointers to Derived-Class 
  978. Pointers</a>  <br>
  979. <a href="#s5p0">9.5<spacer width=20 height=1>Using Member Functions</a>  <br>
  980. <a href="#s6p0">9.6<spacer width=20 height=1>Overriding Base-Class Members in a Derived Class</a>  <br>
  981. <a href="#s7p0">9.7<spacer width=20 height=1>Public, Protected and Private Inheritance</a>  <br>
  982. <a href="#s8p0">9.8<spacer width=20 height=1>Direct Base Classes and Indirect Base Classes</a>  <br>
  983. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  984. <foreign  name="quotes" url="^Quotes::c:s0p0">
  985.  
  986. </page>
  987. <page>
  988. <a href="#s9p0">9.9<spacer width=20 height=1>Using Constructors and Destructors in Derived 
  989. Classes</a>  <br>
  990. <a href="#s10p0">9.10<spacer width=20 height=1>Implicit Derived-Class Object to Base-Class 
  991. Object Conversion</a>  <br>
  992. <a href="#s11p0">9.11<spacer width=20 height=1>Software Engineering with Inheritance</a>  <br>
  993. <a href="#s12p0">9.12<spacer width=20 height=1>Composition Vs. Inheritance</a>  <br>
  994. <a href="#s13p0">9.13<spacer width=20 height=1>"Uses A" and "Knows A" Relationships</a>  <br>
  995. <a href="#s14p0">9.14<spacer width=20 height=1>Case Study: Point, Circle, Cylinder</a>  <br>
  996. <a href="#s15p0">9.15<spacer width=20 height=1>Multiple Inheritance</a>  <br>
  997. <a href="#s16p0">9.16<spacer width=20 height=1>Summary</a>  <br>
  998. <a href="^Terminology::c:s0p0">Terminology</a>  <br>
  999. <a href="^Illustration::c:s0p0">Figures</a>  <br>
  1000. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  1001. <foreign  name="quotes" url="^Quotes::c:s0p0">
  1002.  
  1003. </page>
  1004. </section>
  1005. <section type=Body name=Default title="9.1 Introduction">
  1006. <page>
  1007. <font size=18 bold>9.1 Introduction</font><hr>
  1008. (Note: A number of the techniques described here and 
  1009. in Chapter 10 will be changing as the C++ community 
  1010. gradually begins adopting new techniques as specified 
  1011. in the ANSI/ISO draft standard. We discuss the new 
  1012. techniques, such as run-time type identification (RTTI), 
  1013. in Chapter 21.)<br>
  1014. <spacer width=16 height=1>In this and the next chapter we discuss two of the most 
  1015. important capabilities object-oriented programming 
  1016. provides--<i>inheritance</i> and <i>polymorphism</i>. Inheritance 
  1017. is a form of software reusability in which new classes 
  1018. are created from existing classes by absorbing their <br>
  1019.  
  1020. </page>
  1021. <page>
  1022. attributes and behaviors and overriding or embellishing 
  1023. these with capabilities the new classes require. 
  1024. Software reusability saves time in program 
  1025. development. It encourages the reuse of proven and 
  1026. debugged high-quality software, thus reducing 
  1027. problems after a system becomes functional. These are 
  1028. exciting possibilities. Polymorphism enables us to write 
  1029. programs in a general fashion to handle a wide variety 
  1030. of existing and yet-to-be-specified related classes. 
  1031. Inheritance and polymorphism are effective techniques 
  1032. for dealing with software complexity.<br>
  1033. <spacer width=16 height=1>When creating a new class, instead of writing 
  1034. completely new data members and member functions, <br>
  1035.  
  1036. </page>
  1037. <page>
  1038. the programmer can designate that the new class is to 
  1039. <i>inherit</i> the data members and member functions of a 
  1040. previously defined <i>base class</i>. The new class is referred 
  1041. to as a <i>derived class</i>. Each derived class itself becomes 
  1042. a candidate to be a base class for some future derived 
  1043. class. With <i>single inheritance</i>, a class is derived from 
  1044. one base class. With <i>multiple inheritance</i>, a derived 
  1045. class inherits from multiple (possibly unrelated) base 
  1046. classes. Single inheritance is straightforward--we show 
  1047. several examples that should enable the reader to 
  1048. become proficient quickly. Multiple inheritance is 
  1049. complex and error prone--we show only a simple 
  1050. example and issue a strong caution urging the reader to <br>
  1051.  
  1052. </page>
  1053. <page>
  1054. pursue further study before using this powerful 
  1055. capability.<br>
  1056. <spacer width=16 height=1>A derived class can add data members and member 
  1057. functions of its own, so a derived class can be larger 
  1058. than its base class. A derived class is more specific than 
  1059. its base class and represents a smaller group of objects. 
  1060. With single inheritance, the derived class starts out 
  1061. essentially the same as the base class. The real strength 
  1062. of inheritance comes from the ability to define in the 
  1063. derived class additions, replacements or refinements to 
  1064. the features inherited from the base class. <br>
  1065. <spacer width=16 height=1>C++ offers three kinds of inheritance--<b>public</b>, 
  1066. <b>protected</b> and <b>private</b>. In this chapter we concentrate <br>
  1067.  
  1068. </page>
  1069. <page>
  1070. on <tt><b>public</b></tt> inheritance and briefly explain the other 
  1071. two kinds. In Chapter 15 we show how <b>private</b> 
  1072. inheritance can be used as an alternate form of 
  1073. composition. The third form, <b>protected</b> inheritance, is a 
  1074. relatively recent addition to C++ and is rarely used. 
  1075. With <b>public</b> inheritance, every object of a derived class 
  1076. may also be thought of as an object of that derived 
  1077. class's base class. However, the converse is not true--
  1078. base-class objects are not objects of that base class's 
  1079. derived classes. We will take advantage of this 
  1080. "derived-class-object-is-a-base-class-object" 
  1081. relationship to perform some interesting manipulations. 
  1082. For example, we can thread a wide variety of different <br>
  1083.  
  1084. </page>
  1085. <page>
  1086. objects related through inheritance into a linked list of 
  1087. base-class objects. This allows a variety of objects to be 
  1088. processed in a general way. As we will see in the next 
  1089. chapter, this capability--called polymorphism--is a 
  1090. key thrust of object-oriented programming.<br>
  1091. <spacer width=16 height=1>We add a new form of member access control in this 
  1092. chapter, namely <b>protected</b> access. Derived classes and 
  1093. their <tt><b>friend</b></tt>s can access <b>protected</b> base-class members, 
  1094. whereas non-<b>friend</b>, non-derived-class-member 
  1095. functions cannot.<br>
  1096. <spacer width=16 height=1>Experience in building software systems indicates that 
  1097. significant portions of the code deal with closely related 
  1098. special cases. It becomes difficult in such systems to <br>
  1099.  
  1100. </page>
  1101. <page>
  1102. see the "big picture" because the designer and the 
  1103. programmer become preoccupied with the special 
  1104. cases. Object-oriented programming provides several 
  1105. ways of "seeing the forest through the trees"--a process 
  1106. called <i>abstraction</i>.<br>
  1107. <spacer width=16 height=1>If a program is loaded with closely related special 
  1108. cases, then it is common to see <b>switch</b> statements that 
  1109. distinguish among the special cases and provide the 
  1110. processing logic to deal with each case individually. In 
  1111. Chapter 10, we show how to use inheritance and 
  1112. polymorphism to replace such <b>switch</b> logic with 
  1113. simpler logic.<br>
  1114.  
  1115. </page>
  1116. <page>
  1117. We distinguish between "<i>is a" relationships</i> and <i>"has 
  1118. a" relationships</i>. "Is a" is inheritance. In an "is a" 
  1119. relationship, an object of a derived-class type may also 
  1120. be treated as an object of the base-class type. "Has a" is 
  1121. composition (see Fig. 7.4). In a "has a" relationship, a 
  1122. class object <i>has</i> one or more objects of other classes as 
  1123. members. <br>
  1124. <spacer width=16 height=1>A derived class cannot access the <b>private</b> members of 
  1125. its base class; allowing this would violate the 
  1126. encapsulation of the base class. A derived class can, 
  1127. however, access the <b>public</b> and <b>protected</b> members of 
  1128. its base class. Base-class members that should not be 
  1129. accessible to a derived class via inheritance are declared <br>
  1130.  
  1131. </page>
  1132. <page>
  1133. <b>private</b> in the base class. A derived class can access 
  1134. <b>private</b> members of the base class only through access 
  1135. functions provided in the base class's <b>public</b> and 
  1136. <b>protected</b> interfaces. <br>
  1137. <spacer width=16 height=1>One problem with inheritance is that a derived class can 
  1138. inherit <b>public</b> member function implementations that it 
  1139. does not need to have or should expressly not have. 
  1140. When a base-class member implementation is 
  1141. inappropriate for a derived class, that member can be 
  1142. overridden in the derived class with an appropriate 
  1143. implementation. In some cases, <b>public</b> inheritance is 
  1144. simply inappropriate.<br>
  1145.  
  1146. </page>
  1147. <page>
  1148. Perhaps most exciting is the notion that new classes can 
  1149. inherit from existing <i>class libraries</i>. Organizations 
  1150. develop their own class libraries and can take advantage 
  1151. of other libraries available worldwide. Eventually, 
  1152. software will be constructed predominantly from 
  1153. <i>standardized reusable components</i> just as hardware is 
  1154. often constructed today. This will help to meet the 
  1155. challenges of developing the ever more powerful 
  1156. software we will need in the future.<br>
  1157.  
  1158. </page>
  1159. <page>
  1160. <b>Select the true statement(s). </b><br>
  1161. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Derived classes are more specific than base classes.">
  1162. Base classes are more specific than derived classes.   <br>
  1163. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1164. C++ provides three types of inheritance: public, protected, and private.  <br>
  1165. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1166. The "has a" relationship represents composition.  <br>
  1167. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A derived class can be a base class for another class.">
  1168. A derived class cannot serve as a base class for another class.   <br>
  1169. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1170.  
  1171. </page>
  1172. </section>
  1173. <section type=Body name=Default title="9.2 Inheritance: Base Classes and Derived Classes">
  1174. <page>
  1175. <font size=18 bold>9.2 Inheritance: Base Classes and Derived 
  1176. Classes</font><hr>
  1177. Often an object of one class really "is an" object of 
  1178. another class as well. A rectangle certainly <i>is a</i> 
  1179. quadrilateral (as is a square, a parallelogram and a 
  1180. trapezoid). Thus, class <b>Rectangle</b> can be said to <i>inherit</i> 
  1181. from class <b>Quadrilateral</b>. In this context, class 
  1182. <b>Quadrilateral</b> is called a <i>base class</i> and class 
  1183. <b>Rectangle</b> is called a <i>derived class</i>. A rectangle <i>is a</i> 
  1184. specific type of quadrilateral, but it is incorrect to claim 
  1185. that a quadrilateral<i> is a</i> rectangle (the quadrilateral <br>
  1186.  
  1187. </page>
  1188. <page>
  1189. could, for example, be a parallelogram). <a href="^Illustration::c:s0p1"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 9.1</a> 
  1190. shows several simple inheritance examples.<br>
  1191. <spacer width=16 height=1>Other object-oriented programming languages such as 
  1192. Smalltalk use different terminology: In inheritance, the 
  1193. base class is called the <i>superclass</i> (represents a superset 
  1194. of objects) and the derived class is called the <i>subclass</i> 
  1195. (represents a subset of objects). <br>
  1196. <spacer width=16 height=1>Because inheritance normally produces derived classes 
  1197. with <i>more</i> features than their base classes, the terms 
  1198. superclass and subclass can be confusing; we will avoid 
  1199. these terms. Because derived class objects may be 
  1200. thought of as objects of their base classes, this implies 
  1201. that more objects are associated with base classes and <br>
  1202.  
  1203. </page>
  1204. <page>
  1205. fewer objects are associated with derived classes, so it 
  1206. is reasonable to call base classes "superclasses" and 
  1207. derived classes "subclasses."<br>
  1208. <spacer width=16 height=1>Inheritance forms tree-like hierarchical structures. A 
  1209. base class exists in a hierarchical relationship with its 
  1210. derived classes. A class can certainly exist by itself, but 
  1211. it is when a class is used with the mechanism of 
  1212. inheritance that the class becomes either a base class 
  1213. that supplies attributes and behaviors to other classes, 
  1214. or the class becomes a derived class that inherits 
  1215. attributes and behaviors.<br>
  1216. <spacer width=16 height=1>Let us develop a simple inheritance hierarchy. A typical 
  1217. university community has thousands of people who are <br>
  1218.  
  1219. </page>
  1220. <page>
  1221. community members. These people consist of 
  1222. employees, students and alumni. Employees are either 
  1223. faculty members or staff members. Faculty members 
  1224. are either administrators (such as deans and department 
  1225. chairpersons) or teaching faculty. This yields the 
  1226. inheritance hierarchy shown in <a href="^Illustration::c:s0p3"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 9.2</a>. Note that some 
  1227. administrators also teach classes, so we have used 
  1228. multiple inheritance to form class 
  1229. <b>AdministratorTeacher</b>. Because students often work 
  1230. for their universities, and because employees often take 
  1231. courses, it would also be reasonable to use multiple 
  1232. inheritance to create a class called <b>EmployeeStudent</b>.<br>
  1233.  
  1234. </page>
  1235. <page>
  1236. Another substantial inheritance hierarchy is the <b>Shape</b> 
  1237. hierarchy of <a href="^Illustration::c:s0p2"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 9.3</a>. A common observation among 
  1238. students learning object-oriented programming is that 
  1239. there are abundant examples of hierarchies in the real 
  1240. world. It is just that these students are not accustomed 
  1241. to categorizing the real world in this manner, so it takes 
  1242. some adjustment in their thinking.<br>
  1243. <spacer width=16 height=1>Let us consider the syntax for indicating inheritance. To 
  1244. specify that class <b>CommissionWorker</b> is derived from 
  1245. class <b>Employee</b>, class <b>CommissionWorker</b> would 
  1246. typically be defined as follows<br>
  1247.  
  1248. </page>
  1249. <page>
  1250. <font size=2><br></font><font size=11><pre>
  1251. class CommissionWorker : public Employee {<p>
  1252.    ...<p>
  1253. };<p>
  1254. </pre></font>
  1255. This is called <b><i>public</i></b> <i>inheritance</i> and is the most 
  1256. commonly used type of inheritance. We will also 
  1257. discuss <b><i>private inheritance</i></b> and <b><i>protected inheritance</i></b>. 
  1258. With <b>public</b> inheritance, the <b>public</b> and <b>protected</b> 
  1259. members of the base class are inherited as <b>public</b> and 
  1260. <b>protected</b> members of the derived class, respectively. 
  1261. Remember that <b>private</b> members of a base class are not 
  1262. accessible from that class's derived classes. Note that 
  1263. <b>friend</b> functions are not inherited.<br>
  1264.  
  1265. </page>
  1266. <page>
  1267. It is possible to treat base-class objects and derived-
  1268. class objects similarly; that commonality is expressed 
  1269. in the attributes and behaviors of the base class. Objects 
  1270. of any class derived with <b>public</b> inheritance from a 
  1271. common base class can all be treated as objects of that 
  1272. base class. We will consider many examples in which 
  1273. we can take advantage of this relationship with an ease 
  1274. of programming not available in non-object-oriented 
  1275. languages, such as C.<br>
  1276.  
  1277. </page>
  1278. <page>
  1279. <b>Select the true statement(s). </b><br>
  1280. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A derived class is sometimes referred to as a subclass.">
  1281. A derived class is sometimes referred to as a superclass, because it contains information from multiple classes. <br>
  1282. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A : is used to indicate inheritance. C++ does not provide a keyword inherits.">
  1283. The inherits keyword is used to indicate inheritance. <br>
  1284. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1285. Inheritance forms tree-like hierarchal relationships between data types.  <br>
  1286. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1287.  
  1288. </page>
  1289. </section>
  1290. <section type=Body name=Default title="9.3 Protected Members">
  1291. <page>
  1292. <font size=18 bold>9.3 Protected Members</font><hr>
  1293. A base class's <b>public</b> members are accessible by all 
  1294. functions in the program. A base class's <b>private</b> 
  1295. members are accessible only by member functions and 
  1296. <b>friend</b>s of the base class. <br>
  1297. <spacer width=16 height=1>We introduced <b>protected</b> access as an intermediate 
  1298. level of protection between <b>public</b> access and <b>private</b> 
  1299. access. A base class's <b>protected</b> members may be 
  1300. accessed only by members and <b>friend</b>s of the base class 
  1301. and by members and <b>friend</b>s of derived classes. 
  1302. Derived-class members can refer to <b>public</b> and 
  1303. <b>protected</b> members of the base class simply by using <br>
  1304.  
  1305. </page>
  1306. <page>
  1307. the member names. Note that <b>protected</b> <b><a href="^Engineer::c:s0p0"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a></b>data "breaks" 
  1308. encapsulation--a change to <b>protected</b> members of a 
  1309. base class may require modification of all derived 
  1310. classes. <br>
  1311.  
  1312. </page>
  1313. <page>
  1314. <b>Select the true statement(s). </b><br>
  1315. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1316. Derived class functions can directly access base class members designated public and protected.  <br>
  1317. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A friend member of any class can access that class's protected members.">
  1318. A friend member of the base class cannot access protected members. <br>
  1319. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1320.  
  1321. </page>
  1322. </section>
  1323. <section type=Body name=Default title="9.4 Casting Base-Class Pointers to Derived-Class Pointers">
  1324. <page>
  1325. <font size=18 bold>9.4 Casting Base-Class Pointers to Derived-
  1326. Class Pointers</font><hr>
  1327. An object of a publicly derived class can also be treated 
  1328. as an object of its corresponding base class. This makes 
  1329. possible some interesting manipulations. For example, 
  1330. despite the fact that objects of a variety of classes 
  1331. derived from a particular base class may be quite 
  1332. different from one another, we can still create a linked 
  1333. list of them--again, as long as we treat them as base-
  1334. class objects.  <a href="^Errors::c:s0p0"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>But the reverse is not true: A base-class 
  1335. object is not also automatically a derived-class object. <br>
  1336.  
  1337. </page>
  1338. <page>
  1339. The programmer may, however, use an explicit cast to 
  1340. convert a base-class pointer to a derived-class pointer. 
  1341. <a href="^Errors::c:s0p1"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>But be careful--if such a pointer is to be dereferenced, 
  1342. then the programmer should be sure that the type of the 
  1343. pointer matches the type of the object to which it points. 
  1344. Our treatment in this section uses techniques widely 
  1345. available in most compilers. In Chapter 21, we revisit 
  1346. many of these topics in the context of the latest 
  1347. compilers that conform to recent features of the ANSI/
  1348. ISO C+ draft standard, such as run-time type 
  1349. identification (RTTI), <b>dynamic_cast</b> and <b>typeid</b>.<br>
  1350. <spacer width=16 height=1>Our first example is shown in <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, parts 1 through 
  1351. 6. Parts 1 and 2 show the <b>Point</b> class definition and <br>
  1352.  
  1353. </page>
  1354. <page>
  1355. <b>Point</b> member function definitions. Parts 3 and 4 show 
  1356. the <b>Circle</b> class definition and <b>Circle</b> member function 
  1357. definitions. Part 5 shows a driver program in which we 
  1358. demonstrate assigning derived-class pointers to base-
  1359. class pointers and casting base-class pointers to 
  1360. derived-class pointers. Part 6 shows the program 
  1361. output.<br>
  1362. <spacer width=16 height=1>Let us first examine the <b>Point</b> class definition (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, 
  1363. part 1). The <b>public</b> interface to <b>Point</b> includes member 
  1364. functions <b>setPoint</b>, <b>getX</b> and <b>getY</b>. The data members <b>x</b> 
  1365. and <b>y</b> of <b>Point</b> are specified as <b>protected</b>. This prevents 
  1366. clients of <b>Point</b> objects from directly accessing the 
  1367. data, but enables classes derived from <b>Point</b> to access <br>
  1368.  
  1369. </page>
  1370. <page>
  1371. the inherited data members directly. If the data were 
  1372. <b>private</b>, the <b>public</b> member functions of <b>Point</b> would 
  1373. need to be used to access the data, even by derived 
  1374. classes. Note that the <b>Point</b> overloaded stream-insertion 
  1375. operator function (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, part 2) is able to reference 
  1376. variables <b>x</b> and <b>y</b> directly because the overloaded 
  1377. stream-insertion operator function is a <b>friend</b> of class 
  1378. <b>Point</b>. Note also that it is necessary to reference <b>x</b> and <b>y</b> 
  1379. through objects as in <b>p.x</b> and <b>p.y</b>. This is because the 
  1380. overloaded stream-insertion operator function is not a 
  1381. member function of the class <b>Point</b> so we must use an 
  1382. explicit handle so the compiler knows what object we 
  1383. are referencing. Note that this class offers inlined <br>
  1384.  
  1385. </page>
  1386. <page>
  1387. <b>public</b> member functions <b>getX</b> and <b>getY</b> so 
  1388. <b>operator<<</b> does not need to be a <b>friend</b> to achieve 
  1389. good performance. However, needed <b>public</b> member 
  1390. functions may not be provided in the <b>public</b> interface of 
  1391. every class, so friendship is often appropriate.<br>
  1392. <spacer width=16 height=1>Class <b>Circle</b> (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, part 3) inherits from class <b>Point</b> 
  1393. with <b>public</b> inheritance. This is specified in the first 
  1394. line of the class definition<br>
  1395. <font size=2><br></font><font size=11><pre>
  1396. class Circle : public Point {  <p>
  1397.     // Circle inherits from Point<p>
  1398. </pre></font>
  1399. The colon (<b>:</b>) in the header of the class definition 
  1400. indicates inheritance. The keyword <b>public</b> indicates the 
  1401. type of inheritance. (In<a href="#s7p0"> Section 9.7</a> we will discuss <br>
  1402.  
  1403. </page>
  1404. <page>
  1405. <b>protected</b> and <b>private</b> inheritance.) All the <b>public</b> and 
  1406. <b>protected</b> members of class <b>Point</b> are inherited as 
  1407. <b>public</b> and <b>protected</b> members, respectively, into class 
  1408. <b>Circle</b>. This means that the <b>public</b> interface to <b>Circle</b> 
  1409. includes the <b>Point</b> <b>public</b> members as well as the 
  1410. <b>Circle</b> <b>public</b> members <b>area</b>, <b>setRadius</b> and 
  1411. <b>getRadius</b>. <br>
  1412. The <b>Circle</b> constructor (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, part 4) must invoke 
  1413. the <b>Point</b> constructor to initialize the <b>Point</b> base-class 
  1414. portion of a <b>Circle</b> object. This is accomplished with a 
  1415. member initializer (introduced in Chapter 7) as follows<br>
  1416. <font size=2><br></font><font size=11><pre>
  1417. Circle::Circle( double r, int a, int b )<p>
  1418.    : Point( a, b ) // call base-class constructor<p>
  1419. </pre></font>
  1420.  
  1421. </page>
  1422. <page>
  1423. The second line of the constructor function header 
  1424. invokes the <b>Point</b> constructor by name. Values <b>a</b> and <b>b</b> 
  1425. are passed from the <b>Circle</b> constructor to the <b>Point</b> 
  1426. constructor to initialize the base-class members <b>x</b> and <b>y</b>. 
  1427. If the <b>Circle</b> constructor did not invoke the <b>Point</b> 
  1428. constructor explicitly, the default <b>Point</b> constructor 
  1429. would be invoked implicitly with the default values for 
  1430. <b>x</b> and <b>y</b> (i.e., 0 and 0). If in this case the <b>Point</b> class did 
  1431. not provide a default constructor, the compiler would 
  1432. issue a syntax error. Note that the <b>Circle</b> overloaded 
  1433. <b>operator<<</b> function is able to output the <b>Point</b> part of 
  1434. the <b>Circle</b> by casting the <b>Circle</b> reference <b>c</b> to a <b>Point</b>. 
  1435. This results in a call to <b>operator<<</b> for <b>Point</b> and <br>
  1436.  
  1437. </page>
  1438. <page>
  1439. outputs the <b>x</b> and <b>y</b> coordinates using the proper <b>Point</b> 
  1440. formatting.<br>
  1441. <spacer width=16 height=1>The driver program (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, part 5) creates <b>pointPtr</b> 
  1442. as a pointer to a <b>Point</b> object and instantiates <b>Point</b> 
  1443. object <b>p</b>, then creates <b>circlePtr</b> as a pointer to a <b>Circle</b> 
  1444. object and instantiates <b>Circle</b> object <b>c</b>. The objects <b>p</b> 
  1445. and <b>c</b> are output using their overloaded stream-insertion 
  1446. operators to show that they were initialized correctly. 
  1447. Next, the driver assigns a derived-class pointer (the 
  1448. address of object <b>c</b>) to base-class pointer <b>pointPtr</b> and 
  1449. outputs the <b>Circle</b> object <b>c</b> using <b>operator<<</b> for <b>Point</b> 
  1450. and the dereferenced pointer <b>*pointPtr</b>. Note that only 
  1451. the <b>Point</b> portion of the <b>Circle</b> object <b>c</b> is displayed. <br>
  1452.  
  1453. </page>
  1454. <page>
  1455. With <b>public</b> inheritance, it is always valid to assign a 
  1456. derived-class pointer to a base-class pointer because a 
  1457. derived-class object<i> is a</i> base-class object. The base-
  1458. class pointer "sees" only the base-class part of the 
  1459. derived-class object. The compiler performs an implicit 
  1460. conversion of the derived-class pointer to a base-class 
  1461. pointer. <br>
  1462. <spacer width=16 height=1>Then, the driver program demonstrates assigning a 
  1463. derived-class pointer (the address of object <b>c</b>) to base-
  1464. class pointer <b>pointPtr</b> and casting <b>pointPtr</b> back to a 
  1465. <b>Circle *</b>. The result of the cast operation is assigned to 
  1466. <b>circlePtr</b>. The <b>Circle</b> object <b>c</b> is output using the 
  1467. overloaded stream-insertion operator for <b>Circle</b> and the <br>
  1468.  
  1469. </page>
  1470. <page>
  1471. dereferenced pointer <b>*circlePtr.</b> The area of <b>Circle</b> 
  1472. object <b>c</b> is output via <b>circlePtr</b>. This results in a valid 
  1473. area value because the pointers are always pointing to a 
  1474. <b>Circle</b> object. <br>
  1475. <spacer width=16 height=1>A base-class pointer cannot be assigned directly to a 
  1476. derived-class pointer because this is an inherently 
  1477. dangerous assignment--derived-class pointers expect 
  1478. to be pointing to derived-class objects. The compiler 
  1479. does not perform an implicit conversion in this case. 
  1480. Using an explicit cast informs the compiler that the 
  1481. programmer knows this type of pointer conversion is 
  1482. dangerous--the programmer assumes responsibility for <br>
  1483.  
  1484. </page>
  1485. <page>
  1486. using the pointer appropriately, so the compiler is 
  1487. willing to allow the dangerous conversion.<br>
  1488. <spacer width=16 height=1>Next, the driver assigns a base-class pointer (the 
  1489. address of object <b>p</b>) to base-class pointer <b>pointPtr</b> and 
  1490. casts <b>pointPtr</b> back to a <b>Circle *</b>. The result of the cast 
  1491. operation is assigned to <b>circlePtr</b>. <b>Point</b> object <b>p</b> is 
  1492. output using <tt><b>operator<<</b></tt> for <b>Circle</b> and the 
  1493. dereferenced pointer <b>*circlePtr</b>. Note the zero value 
  1494. output for the radius member (which actually does not 
  1495. exist because <b>circlePtr</b> is really aimed at a <b>Point</b> 
  1496. object). Outputting a <b>Point</b> as a <b>Circle</b> results in an 
  1497. undefined value (in this case it happens to be zero) for 
  1498. the <b>radius</b> because the pointers are always pointing to a <br>
  1499.  
  1500. </page>
  1501. <page>
  1502. <b>Point</b> object. A <b>Point</b> object does not have a <b>radius</b> 
  1503. member. Therefore, the program outputs whatever 
  1504. value happens to be in memory at the location that 
  1505. <b>circlePtr</b> expects the <b>radius</b> data member to be. The 
  1506. area of the object pointed to by <b>circlePtr</b> (<b>Point</b> object 
  1507. <b>p</b>) is also output via <b>circlePtr</b>. Note that the value for 
  1508. the area is 0.00 because this calculation is based on the 
  1509. "undefined" value of the <b>radius</b>. Obviously, accessing 
  1510. data members that are not there is dangerous. Calling 
  1511. member functions that do not exist can crash a program.<br>
  1512. <spacer width=16 height=1>In this section we have shown the mechanics of pointer 
  1513. conversions. This material establishes the foundation <br>
  1514.  
  1515. </page>
  1516. <page>
  1517. we will need for our deeper treatment of object-oriented 
  1518. programming with polymorphism in the next chapter.<br>
  1519.  
  1520. </page>
  1521. <page>
  1522. <b>Select the true statement(s). </b><br>
  1523. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A base class object is not considered to be a derived class object.">
  1524. A base class object "is a" derived class object.   <br>
  1525. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1526. An object of a publicly derived class can be treated as an object of its corresponding base class.  <br>
  1527. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1528. With public inheritance, it is always possible to assign a derived class pointer to a pointer of its base class type.  <br>
  1529. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1530. A base class pointer cannot be assigned directly to a derived class pointer without an explicit cast operation.  <br>
  1531. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1532.  
  1533. </page>
  1534. </section>
  1535. <section type=Body name=Default title="9.5 Using Member Functions">
  1536. <page>
  1537. <font size=18 bold>9.5 Using Member Functions</font><hr>
  1538. A derived class's member functions may need to access 
  1539. certain of its base class's data members and member 
  1540. functions. <br>
  1541. <spacer width=16 height=1>This is a crucial aspect of software engineering in C++. 
  1542. If a derived class could access the <b><a href="^Engineer::c:s0p1"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a></b>base class's <b>private</b> 
  1543. members, this would violate the encapsulation of the 
  1544. base class. Hiding <b>private</b> members is a huge help in 
  1545. testing, debugging and correctly modifying systems. If 
  1546. a derived class could access its base class's <b>private</b> 
  1547. members, it would then be possible for classes derived 
  1548. from that derived class to access that data as well, and <br>
  1549.  
  1550. </page>
  1551. <page>
  1552. so on. This would propagate access to what is supposed 
  1553. to be <b>private</b> data, and the benefits of encapsulation 
  1554. would be lost throughout the class hierarchy.<br>
  1555.  
  1556. </page>
  1557. </section>
  1558. <section type=Body name=Default title="9.6 Overriding Base-Class Members in a Derived Class">
  1559. <page>
  1560. <font size=18 bold>9.6 Overriding Base-Class Members in a 
  1561. Derived Class</font><hr>
  1562. A derived class can override a base-class member 
  1563. function by supplying a new version of that function 
  1564. with the same signature (if the signature were different, 
  1565. this would be function overloading rather than function 
  1566. overriding). When that function is mentioned by name 
  1567. in the derived class, the derived-class version is 
  1568. automatically selected.  <a href="^Errors::c:s0p2"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>The scope-resolution operator 
  1569. may be used to access the base-class version from the 
  1570. derived class.<br>
  1571.  
  1572. </page>
  1573. <page>
  1574. Consider a simplified class <b>Employee</b>. It stores the 
  1575. employee's <b>firstName</b> and <b>lastName</b>. This information 
  1576. is common to all employees including those in classes 
  1577. derived from class <b>Employee</b>. From class <b>Employee</b> 
  1578. now derive classes <b>HourlyWorker</b>, <b>PieceWorker</b>, 
  1579. <b>Boss</b> and <b>CommissionWorker</b>. The <b>HourlyWorker</b> 
  1580. gets paid by the hour with "time-and-a-half" for 
  1581. overtime hours in excess of 40 hours per week. The 
  1582. <b>PieceWorker</b> gets paid a fixed rate per item 
  1583. produced--for simplicity, assume this person makes 
  1584. only one type of item, so the <b>private</b> data members are 
  1585. number of items produced and rate per item. The <b>Boss</b> 
  1586. gets a fixed wage per week. The <b>CommissionWorker</b> <br>
  1587.  
  1588. </page>
  1589. <page>
  1590. gets a small fixed weekly base salary plus a fixed 
  1591. percentage of that person's gross sales for the week. For 
  1592. simplicity, we study only class <b>Employee</b> and derived 
  1593. class <b>HourlyWorker</b>.<br>
  1594. <spacer width=16 height=1>Our next example is shown in <a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.5</a>, parts 1 through 
  1595. 5. Parts 1 and 2 show the <b>Employee</b> class definition and 
  1596. <b>Employee</b> member function definitions. Parts 3 and 4 
  1597. show the <b>HourlyWorker</b> class definition and 
  1598. <b>HourlyWorker</b> member function definition. Part 5 
  1599. shows a driver program for the <b>Employee</b>/
  1600. <b>HourlyWorker</b> inheritance hierarchy that simply 
  1601. instantiates an <b>HourlyWorker</b> object, initializes it and <br>
  1602.  
  1603. </page>
  1604. <page>
  1605. calls <b>HourlyWorker</b> member function <b>print</b> to output 
  1606. the object's data. <br>
  1607. <spacer width=16 height=1>The <b>Employee</b> class definition (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.5</a>, part 1) 
  1608. consists of two private <b>char *</b> data members--
  1609. <b>firstName</b> and <b>lastName</b>--and three member 
  1610. functions--a constructor, a destructor and <b>print</b>. The 
  1611. constructor function (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.5</a>, part 2) receives two 
  1612. strings and dynamically allocates character arrays to 
  1613. store the strings. Note that the <b>assert</b> macro (discussed 
  1614. in Chapter 18, "Other Topics") is used to determine if 
  1615. memory was allocated to <b>firstName</b> and <b>lastName</b>. If 
  1616. not, the program terminates with an error message 
  1617. indicating the condition tested, the line number on <br>
  1618.  
  1619. </page>
  1620. <page>
  1621. which the condition appears and the file in which the 
  1622. condition is located. [Note, once again, that in the C++ 
  1623. draft standard, <b>new</b> "throws" an exception if 
  1624. insufficient memory is available; we discuss this in 
  1625. Chapter 13.] Because the data of <b>Employee</b> are 
  1626. <b>private</b>, the only access to the data is through member 
  1627. function <b>print</b> which simply outputs the first name and 
  1628. last name of the employee. The destructor function 
  1629. returns the dynamically allocated memory to the system 
  1630. (to avoid a "memory leak").<br>
  1631. <spacer width=16 height=1>Class <b>HourlyWorker</b> (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.5</a>, part 3) inherits from 
  1632. class <b>Employee</b> with <b>public</b> inheritance. Again, this is <br>
  1633.  
  1634. </page>
  1635. <page>
  1636. specified in the first line of the class definition using the 
  1637. colon (<tt><b>:</b></tt>) notation as follows<br>
  1638. <font size=2><br></font><font size=11><pre>
  1639. class HourlyWorker : public Employee<p>
  1640. </pre></font>
  1641. The <b>public</b> interface to <b>HourlyWorker</b> includes the 
  1642. <b>Employee</b> <b>print</b> function and <b>HourlyWorker</b> member 
  1643. functions <b>getPay</b> and <b>print</b>. Note that class 
  1644. <b>HourlyWorker</b> defines its own <b>print</b> function with the 
  1645. same prototype as <b>Employee::print()</b>--this is an 
  1646. example of function overriding. Therefore, class 
  1647. <b>HourlyWorker</b> has access to two <b>print</b> functions. 
  1648. Class <b>HourlyWorker</b> also contains <b>private</b> data <br>
  1649.  
  1650. </page>
  1651. <page>
  1652. members <b>wage</b> and <b>hours</b> for calculating the 
  1653. employee's weekly salary.<br>
  1654. <spacer width=16 height=1>The <b>HourlyWorker</b> constructor (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.5</a>, part 4) uses 
  1655. member initializer syntax to pass the strings <b>first</b> and 
  1656. <b>last</b> to the <b>Employee</b> constructor so the base-class 
  1657. members can be initialized, then initializes members 
  1658. <b>hours</b> and <b>wage</b>. Member function <b>getPay</b> calculates 
  1659. the salary of the <b>HourlyWorker</b>. <br>
  1660. <spacer width=16 height=1><b>HourlyWorker</b> member function <b>print</b> overrides the 
  1661. <b>Employee</b> <b>print</b> member function. Often, base-class 
  1662. member functions are overridden in a derived class to 
  1663. provide more functionality. The overridden functions 
  1664. sometimes call the base-class version of the function to <br>
  1665.  
  1666. </page>
  1667. <page>
  1668. perform part of the new task. In this example, the 
  1669. derived-class <b>print</b> function calls the base-class <b>print</b> 
  1670. function to output the employee's name (the base-class 
  1671. <b>print</b> is the only function with access to the <b>private</b> 
  1672. data of the base class). The derived-class <b>print</b> function 
  1673. also outputs the employee's pay. Note how the base-
  1674. class version of <b>print</b> is called<br>
  1675. <font size=2><br></font><font size=11><pre>
  1676. Employee::print();<p>
  1677. </pre></font>
  1678. Because the base-class function and the derived-class 
  1679. function have the same name and signature, the base-
  1680. class function must be preceded by its class name and 
  1681. the scope resolution operator. Otherwise, the derived-
  1682. class version of the function would be called causing <br>
  1683.  
  1684. </page>
  1685. <page>
  1686. infinite recursion (i.e., the <b>HourlyWorker</b> <b>print</b> 
  1687. function would call itself).<br>
  1688.  
  1689. </page>
  1690. <page>
  1691. <b>Select the true statement(s). </b><br>
  1692. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Base class functions can be overridden.">
  1693. A base class function cannot be overridden by a derived class.   <br>
  1694. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1695. An overridden function will sometimes call the base class version.  <br>
  1696. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1697. Functions can be overridden at many levels in an inheritance hierarchy.  <br>
  1698. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1699.  
  1700. </page>
  1701. </section>
  1702. <section type=Body name=Default title="9.7 Public, Protected and Private Inheritance">
  1703. <page>
  1704. <font size=18 bold>9.7 Public, <a href="$currentLink"></a>Protected and Private Inheritance</font><hr>
  1705. When deriving a class from a base class, the base class 
  1706. may be inherited as <b>public</b>, <b>protected</b> or <b>private</b>. Use 
  1707. of <b>protected</b> and <b>private</b> inheritance is rare and each 
  1708. should be used only with great care; we normally use 
  1709. <b>public</b> inheritance in this book (Chapter 15 
  1710. demonstrates<tt><b> private</b></tt> inheritance as another form of 
  1711. composition). <a href="^Illustration::c:s0p5"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 9.6</a> summarizes for each type of 
  1712. inheritance the accessibility of base-class members in a 
  1713. derived class. The first column contains the base-class 
  1714. member-access specifiers.<br>
  1715.  
  1716. </page>
  1717. <page>
  1718. When deriving a class from a <b>public</b> base class, <b>public</b> 
  1719. members of the base class become <b>public</b> members of 
  1720. the derived class and <b>protected</b> members of the base 
  1721. class become <b>protected</b> members of the derived class. 
  1722. A base class's <b>private</b> members are never directly 
  1723. accessible from a derived class but can be accessed 
  1724. through calls to the <b>public</b> and <b>protected</b> members of 
  1725. the base class.<br>
  1726. <spacer width=16 height=1>When deriving from a <b>protected</b> base class, <b>public</b> and 
  1727. <b>protected</b> members of the base class become <b>protected</b> 
  1728. members of the derived class. When deriving from a 
  1729. <b>private</b> base class, <b>public</b> and <b>protected</b> members of 
  1730. the base class become <b>private</b> members (e.g., the <br>
  1731.  
  1732. </page>
  1733. <page>
  1734. functions become utility functions) of the derived class. 
  1735. <b>Private</b> and <b>protected</b> inheritance are not "is a" 
  1736. relationships.  <br>
  1737.  
  1738. </page>
  1739. <page>
  1740. <b>Select the true statement(s). </b><br>
  1741. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. They become protected.">
  1742. When deriving from a protected base class, public and protected members of the base class become private members in the derived class.   <br>
  1743. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1744. When deriving from a public base class, public members of the base class become public members in the derived class.  <br>
  1745. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1746. When deriving from a public base class, protected members of the base class become protected members in the derived class.  <br>
  1747. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1748.  
  1749. </page>
  1750. </section>
  1751. <section type=Body name=Default title="9.8 Direct Base Classes and Indirect Base Classes">
  1752. <page>
  1753. <font size=18 bold>9.8 Direct Base Classes and Indirect Base 
  1754. Classes</font><hr>
  1755. A base class may be a <i>direct base class</i> of a derived 
  1756. class, or a base class may be an <i>indirect base class</i> of a 
  1757. derived class. A direct base class of a derived class is 
  1758. explicitly listed in that derived class's header with the 
  1759. colon (<b>:</b>) notation when that derived class is declared. 
  1760. An indirect base class is not explicitly listed in the 
  1761. derived class's header; rather the indirect base class is 
  1762. inherited from two or more levels up the class 
  1763. hierarchy.<br>
  1764.  
  1765. </page>
  1766. <page>
  1767. <b>Select the true statement(s). </b><br>
  1768. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1769. The direct base class of a class is the one explicitly listed in the derived class's definition after the : that indicates inheritance.  <br>
  1770. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1771. An indirect base class is not explicitly listed in the derived class's definition, but is inherited in a class two or more levels up the class hierarchy.  <br>
  1772. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1773.  
  1774. </page>
  1775. </section>
  1776. <section type=Body name=Default title="9.9 Using Constructors and Destructors in Derived Classes">
  1777. <page>
  1778. <font size=18 bold>9.9 Using Constructors and Destructors in 
  1779. Derived Classes</font><hr>
  1780. Because a derived class inherits its base class's 
  1781. members, when an object of a derived class is 
  1782. instantiated, the base class's constructor must be called 
  1783. to initialize the base-class members of the derived-class 
  1784. object. <a href="^Engineer::c:s0p4"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>A <i>base-class initializer</i> (which uses the 
  1785. member-initializer syntax we have seen) can be 
  1786. provided in the derived-class constructor to call the 
  1787. <a href="^Engineer::c:s0p2"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>base-class constructor explicitly; otherwise, the derived 
  1788. class's constructor will call the base class's default 
  1789. constructor implicitly.<br>
  1790.  
  1791. </page>
  1792. <page>
  1793. Base-class constructors and base-class assignment 
  1794. operators are not inherited by derived classes. Derived-
  1795. class constructors and assignment operators, however, 
  1796. can call base-class constructors and assignment 
  1797. operators. <br>
  1798. <spacer width=16 height=1> <a href="^Engineer::c:s0p6"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>A derived-class constructor always calls the 
  1799. constructor for its base class first to initialize the 
  1800. derived class's base-class members. If the derived-class 
  1801. constructor is omitted, the derived class's default 
  1802. constructor calls the base-class's default constructor. 
  1803. Destructors are called in the reverse order of 
  1804. constructor calls, so a derived-class destructor is called 
  1805. before its base-class destructor.<br>
  1806.  
  1807. </page>
  1808. <page>
  1809. The program of <a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.7</a> demonstrates the order in 
  1810. which base-class and derived-class constructors and 
  1811. destructors are called. The program consists of five 
  1812. parts. Parts 1 and 2 show a simple <b>Point</b> class 
  1813. containing a constructor, a destructor and <b>protected</b> 
  1814. data members <b>x</b> and <b>y</b>. The constructor and destructor 
  1815. both print the <b>Point</b> object for which they are invoked. <br>
  1816. <spacer width=16 height=1>Parts 3 and 4 of <a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.7</a> show a simple <b>Circle</b> class 
  1817. derived from <b>Point</b> with <b>public</b> inheritance. Class 
  1818. <b>Circle</b> provides a constructor, a destructor and a 
  1819. <b>private</b> data member <b>radius</b>. The constructor and 
  1820. destructor both print the <b>Circle</b> object for which they 
  1821. are invoked. The <b>Circle</b> constructor also invokes the <br>
  1822.  
  1823. </page>
  1824. <page>
  1825. <b>Point</b> constructor using member initializer syntax and 
  1826. passes the values a and b so the base-class data 
  1827. members <b>x</b> and <b>y</b> can be initialized.<br>
  1828. <spacer width=16 height=1>Part 5 of<a href="^Code::c:s0p2"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.7</a> is a driver program for this <b>Point</b>/
  1829. <b>Circle</b> hierarchy. The program begins by instantiating a 
  1830. <b>Point</b> object in its own scope inside <b>main</b>. The object 
  1831. goes in and out of scope immediately, so the <b>Point</b> 
  1832. constructor and destructor are both called. Next, the 
  1833. program instantiates <b>Circle</b> object <b>circle1</b>. This invokes 
  1834. the <b>Point</b> constructor to perform output with values 
  1835. passed from the <b>Circle</b> constructor, then performs the 
  1836. output specified in the <b>Circle</b> constructor. <b>Circle</b> object 
  1837. <b>circle2</b> is instantiated next. Again, the <b>Point</b> and <b>Circle</b> <br>
  1838.  
  1839. </page>
  1840. <page>
  1841. constructors are both called. Note that the body of the 
  1842. <b>Point</b> constructor is performed before the body of the 
  1843. <b>Circle</b> constructor. The end of <b>main</b> is reached, so the 
  1844. destructors are called for objects <b>circle1</b> and <b>circle2</b>. 
  1845. Destructors are called in the reverse order of their 
  1846. corresponding constructors. Therefore, the <b>Circle</b> 
  1847. destructor and <b>Point</b> destructor are called in that order 
  1848. for object <b>circle2</b>, then the <b>Circle</b> and <b>Point</b> destructors 
  1849. are called in that order for object <b>circle1</b>.<br>
  1850.  
  1851. </page>
  1852. <page>
  1853. <b>Select the true statement(s). </b><br>
  1854. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Constructors are not inherited.">
  1855. Base class constructors are inherited.   <br>
  1856. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1857. Derived class constructors must call constructors for the immediate base classes.  <br>
  1858. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1859. A derived class destructor is called before the base class destructor.  <br>
  1860. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1861.  
  1862. </page>
  1863. </section>
  1864. <section type=Body name=Default title="9.10 Implicit Derived-Class Object to Base-Class Object Conversion">
  1865. <page>
  1866. <font size=18 bold>9.10 Implicit Derived-Class Object to Base-
  1867. Class Object Conversion</font><hr>
  1868. Despite the fact that a derived-class object also "is a" 
  1869. base-class object, the derived-class type and the base-
  1870. class type are different. Under <b>public</b> inheritance, 
  1871. derived-class objects can be treated as base-class 
  1872. objects. This makes sense because the derived class has 
  1873. members corresponding to each of the base-class 
  1874. members--remember that the derived class can have 
  1875. more members than the base class has.  <a href="^Errors::c:s0p5"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Assignment in 
  1876. the other direction is not allowed because assigning a 
  1877. base-class object to a derived-class object would leave <br>
  1878.  
  1879. </page>
  1880. <page>
  1881. the additional derived-class members undefined. 
  1882. Although such assignment is not "naturally" allowed, it 
  1883. could be made legitimate by providing a properly 
  1884. overloaded assignment operator and/or conversion 
  1885. constructor (see Chapter 8).<br>
  1886. <spacer width=16 height=1>Note that what we say about pointers in the remainder 
  1887. of this section also applies to references.<br>
  1888. <spacer width=16 height=1>With <b>public</b> inheritance, a pointer to a derived-class 
  1889. object may be implicitly converted into a pointer to a 
  1890. base-class object because a derived-class object is a 
  1891. base-class object.<br>
  1892.  
  1893. </page>
  1894. <page>
  1895. There are four possible ways to mix and match base-
  1896. class pointers and derived-class pointers with base-class 
  1897. objects and derived-class objects:<br>
  1898. 1. Referring to a base-class object with a base-class 
  1899. pointer is straightforward.<br>
  1900. 2. Referring to a derived-class object with a derived-
  1901. class pointer is straightforward.<br>
  1902. 3. Referring to a derived-class object with a base-class 
  1903. pointer is safe because the derived-class object is an 
  1904. object of its base class as well. Such code can only refer 
  1905. to base-class members. If this code refers to derived-
  1906. class-only members through the base-class pointer, the 
  1907. compiler will report a syntax error.<br>
  1908.  
  1909. </page>
  1910. <page>
  1911. 4. R<a href="^Errors::c:s0p6"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>eferring to a base-class object with a derived-class 
  1912. pointer is a syntax error. The derived-class pointer must 
  1913. first be cast to a base-class pointer.<br>
  1914. As convenient as it may be to treat derived-class objects 
  1915. as base-class objects, and to do this by manipulating all 
  1916. these objects with base-class pointers, there is a 
  1917. problem. In a payroll system, for example, we would 
  1918. like to be able to walk through a linked list of 
  1919. employees and calculate the weekly pay for each 
  1920. person. But using base-class pointers enables the 
  1921. program to call only the base-class payroll calculation 
  1922. routine (if indeed there were such a routine in the base 
  1923. class). We need a way to invoke the proper payroll <br>
  1924.  
  1925. </page>
  1926. <page>
  1927. calculation routine for each object, whether it is a base-
  1928. class object or a derived-class object, and to do this 
  1929. simply by using the base-class pointer. The solution is 
  1930. to use <b>virtual</b> functions and polymorphism, as will be 
  1931. discussed in Chapter 10.<br>
  1932.  
  1933. </page>
  1934. <page>
  1935. <b>Select the true statement(s). </b><br>
  1936. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1937. A derived class pointer can refer to a derived class object.  <br>
  1938. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1939. A base class pointer can safely refer to a derived class object's members that were inherited from the base class.   <br>
  1940. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A derived class pointer must be explicitly cast if it is to point to an object of the base class.">
  1941. A derived class pointer can point to an object of the base class without the need for casting. <br>
  1942. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1943.  
  1944. </page>
  1945. </section>
  1946. <section type=Body name=Default title="9.11 Software Engineering with Inheritance">
  1947. <page>
  1948. <font size=18 bold>9.11 Software Engineering with Inheritance</font><hr>
  1949. We can use inheritance to customize existing software. 
  1950. We inherit the attributes and behaviors of an existing 
  1951. class, then add attributes and behaviors (or override 
  1952. base-class behaviors) to customize the class to meet our 
  1953. needs. This is done in C++ without the derived class 
  1954. having access to the base class's source code, but the 
  1955. derived class does need to be able to link to the base 
  1956. class's object code. <a href="^Engineer::c:s0p9"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>This powerful capability is 
  1957. attractive to independent software vendors (ISVs). The 
  1958. ISVs can develop proprietary classes for sale or license 
  1959. and make these classes available to users in object-code <br>
  1960.  
  1961. </page>
  1962. <page>
  1963. format. Users can then derive new classes from these 
  1964. library classes rapidly and without accessing the ISVs' 
  1965. proprietary <a href="^Perform::c:s0p0"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>source code. All the ISVs need to supply 
  1966. with the object code are the header files. <br>
  1967. <spacer width=16 height=1>It can be difficult for students to appreciate the 
  1968. problems faced by designers and implementors on 
  1969. large-scale software projects. People experienced on 
  1970. such projects will invariably state that a key to 
  1971. improving the software development process is 
  1972. software reuse. Object-oriented programming in 
  1973. general, and C++ in particular, certainly do this.<br>
  1974. <spacer width=16 height=1>It is the availability of substantial and useful class 
  1975. libraries that delivers the maximum benefits of software <br>
  1976.  
  1977. </page>
  1978. <page>
  1979. reuse through inheritance. As interest in C++ grows, 
  1980. interest in class libraries is growing exponentially. Just 
  1981. as shrink-wrapped software produced by independent 
  1982. software vendors became an explosive growth industry 
  1983. with the arrival of the personal computer, so, too, is the 
  1984. creation and sale of class libraries. Application 
  1985. designers are building their applications with these 
  1986. libraries, and library designers are being rewarded by 
  1987. having their libraries wrapped with the applications. 
  1988. Libraries currently being shipped with C++ compilers 
  1989. tend to be rather general-purpose and limited in scope. 
  1990. What is coming is a massive worldwide commitment to <br>
  1991.  
  1992. </page>
  1993. <page>
  1994. the development of class libraries for a huge variety of 
  1995. applications arenas.<br>
  1996. <spacer width=16 height=1> <a href="^Engineer::c:s0p12"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>A base class specifies commonality--all classes 
  1997. derived from a base class inherit the capabilities of that 
  1998. base class. In the object-oriented design process, the 
  1999. designer looks for<a href="^Engineer::c:s0p11"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a> commonality and "factors it out" to 
  2000. form desirable base classes. Derived classes are then 
  2001. customized beyond the capabilities inherited from the 
  2002. base class.<br>
  2003. <spacer width=16 height=1>Just as the designer of non-object-oriented systems 
  2004. seeks to avoid unnecessary proliferation of functions, 
  2005. the designer of object-oriented systems should avoid 
  2006. unnecessary proliferation of classes. Such a <br>
  2007.  
  2008. </page>
  2009. <page>
  2010. proliferation of classes creates management problems 
  2011. and can hinder software reusability simply because it is 
  2012. more difficult for a potential reuser of a class to locate 
  2013. that class in a huge collection. <a href="^Perform::c:s0p1"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>The trade-off is to create 
  2014. fewer classes, each providing substantial additional 
  2015. functionality. Such classes might be too rich for certain 
  2016. reusers; they can mask the excessive functionality, thus 
  2017. "toning down" the classes to meet their needs. <br>
  2018. <spacer width=16 height=1> <a href="^Engineer::c:s0p14"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Note that reading a set of derived-class declarations 
  2019. can be confusing because inherited members are not 
  2020. shown, <a href="^Engineer::c:s0p13"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>but they are nevertheless present in the derived 
  2021. classes. A similar problem can exist in the 
  2022. documentation of derived classes.<br>
  2023.  
  2024. </page>
  2025. </section>
  2026. <section type=Body name=Default title="9.12 Composition Vs. Inheritance">
  2027. <page>
  2028. <font size=18 bold>9.12 Composition Vs. Inheritance</font><hr>
  2029. We have discussed <i>is a</i> relationships which are 
  2030. supported by <b>public</b> inheritance. We have also 
  2031. discussed <i>has a</i> relationships (and seen examples in 
  2032. preceding chapters) in which a class may have other 
  2033. classes as members--<a href="^Engineer::c:s0p16"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>such relationships create new 
  2034. classes by <i>composition</i> of existing classes. For example, 
  2035. given the classes <b>Employee</b>, <b>BirthDate</b> and 
  2036. <b>TelephoneNumber</b>, it is improper to say that an 
  2037. <b>Employee</b> <i>is a</i> <b>BirthDate</b> or that an <b>Employee</b> <i>is a</i> 
  2038. <b>TelephoneNumber</b>. But it is certainly appropriate to <br>
  2039.  
  2040. </page>
  2041. <page>
  2042. say that an <b>Employee</b> <i>has a</i> <b>BirthDate</b> and that an 
  2043. <b>Employee</b> <i>has a</i> <b>TelephoneNumber</b>. <br>
  2044.  
  2045. </page>
  2046. </section>
  2047. <section type=Body name=Default title="9.13 "Uses A" and "Knows A" Relationships">
  2048. <page>
  2049. <font size=18 bold>9.13 "Uses A" and "Knows A" Relationships</font><hr>
  2050. Inheritance and composition each encourage software 
  2051. reuse by creating new classes that have much in 
  2052. common with existing classes. There are other ways to 
  2053. utilize the services of classes. Although a person object 
  2054. is not a car, and a person object does not contain a car, a 
  2055. person object certainly <i>uses a</i> car. A function uses an 
  2056. object simply by issuing a function call to a member 
  2057. function of that object. <br>
  2058. <spacer width=16 height=1>An object can be <i>aware of</i> another object. Knowledge 
  2059. networks frequently have such relationships. One 
  2060. object can contain a pointer handle or a reference <br>
  2061.  
  2062. </page>
  2063. <page>
  2064. handle to another object to be aware of that object. In 
  2065. this case, one object is said to have a <i>knows a</i> 
  2066. relationship with the other object; this is sometimes 
  2067. called an <i>association</i>.<br>
  2068.  
  2069. </page>
  2070. </section>
  2071. <section type=Body name=Default title="9.14 Case Study: Point, Circle, Cylinder">
  2072. <page>
  2073. <font size=18 bold>9.14 Case Study: Point, Circle, Cylinder</font><hr>
  2074. Now let us consider the capstone exercise for this 
  2075. chapter. We consider a point, circle, cylinder hierarchy. 
  2076. First we develop and use class <b>Point</b> (<a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.8</a>). Then 
  2077. we present an example in which we derive class <b>Circle</b> 
  2078. from class <b>Point</b> (<a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.9</a>). Finally, we present an 
  2079. example in which we derive class <b>Cylinder</b> from class 
  2080. <b>Circle</b> (<a href="^Code::c:s0p5"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.10</a>).<br>
  2081. <spacer width=16 height=1><a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 9.8</a> shows class <b>Point</b>. Part 1 is the class <b>Point</b> 
  2082. definition. Note that Point's data members are 
  2083. <b>protected</b>. Thus, when class <b>Circle</b> is derived from 
  2084. class <b>Point</b>, the member functions of class <b>Circle</b> will <br>
  2085.  
  2086. </page>
  2087. <page>
  2088. be able to directly reference coordinates <b>x</b> and <b>y</b> rather 
  2089. than using access functions. This may result in better 
  2090. performance.<br>
  2091. <spacer width=16 height=1><a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 9.8</a>, part 2, shows the member function 
  2092. definitions for class <b>Point</b>.<a href="^Code::c:s0p3"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 9.8</a>, part 3, shows a 
  2093. driver program for class <b>Point</b>. Note that <b>main</b> must 
  2094. use the access functions <b>getX</b> and <b>getY</b> to read the 
  2095. values of <b>protected</b> data members <b>x</b> and <b>y</b>; remember 
  2096. that <b>protected</b> data members are accessible only to 
  2097. members and <tt><b>friend</b></tt>s of their class and members and 
  2098. <b>friend</b>s of their derived classes.<br>
  2099. <spacer width=16 height=1>Our next example is shown in <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.9</a>, parts 1 through 
  2100. 5. The <b>Point</b> class definition and the member function <br>
  2101.  
  2102. </page>
  2103. <page>
  2104. definitions from <a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.8</a> are reused here. Parts 1 
  2105. through 5 show the <b>Circle</b> class definition, <b>Circle</b> 
  2106. member function definitions and a driver program with 
  2107. output. Note that class <b>Circle</b> inherits from class <b>Point</b> 
  2108. with <b>public</b> inheritance. This means that the <b>public</b> 
  2109. interface to <b>Circle</b> includes the <b>Point</b> member functions 
  2110. as well as the <b>Circle</b> member functions <b>setRadius</b>, 
  2111. <b>getRadius</b> and <b>area</b>. <br>
  2112. <spacer width=16 height=1>Note that the <b>Circle</b> overloaded <tt><b>operator<<</b></tt> function 
  2113. which is a <b>friend</b> of class <b>Circle</b> is able to output the 
  2114. <b>Point</b> part of the <b>Circle</b> by casting the <b>Circle</b> reference 
  2115. <b>c </b>to a <b>Point</b>. This results in a call to <b>operator<<</b> for <br>
  2116.  
  2117. </page>
  2118. <page>
  2119. <b>Point</b> and outputs the <b>x</b> and <b>y</b> coordinates using the 
  2120. proper <b>Point</b> formatting. <br>
  2121. The driver program in part 4 of <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.9</a> instantiates an 
  2122. object of class <b>Circle</b> then uses <i>get</i> functions to obtain 
  2123. the information about the <b>Circle</b> object. Again, <b>main</b> is 
  2124. neither a member function nor a <b>friend</b> of class <b>Circle</b> 
  2125. so it cannot directly reference the <b>protected</b> data of 
  2126. class <b>Circle</b>. The driver program then uses <i>set</i> functions 
  2127. <b>setRadius</b> and <b>setPoint</b> to reset the radius and 
  2128. coordinates of the center of the circle. Finally, the driver 
  2129. initializes reference variable <b>pRef</b> of type "reference to 
  2130. <b>Point</b> object" (<b>Point &</b>) to <b>Circle</b> object <b>c</b>. The driver 
  2131. then prints <b>pRef</b> which, despite the fact that it is <br>
  2132.  
  2133. </page>
  2134. <page>
  2135. initialized with a <b>Circle</b> object, "thinks" it is a <b>Point</b> 
  2136. object, so the <b>Circle</b> object actually prints as a <b>Point</b> 
  2137. object.<br>
  2138. <spacer width=16 height=1>Our last example is shown in<a href="^Code::c:s0p5"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.10</a>, parts 1 through 
  2139. 5. The <b>Point</b> class and <b>Circle</b> class definitions, and 
  2140. their member function definitions from<a href="^Code::c:s0p3"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.8</a> and 
  2141. <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.9</a> are reused here. Parts 1 through 5 show the 
  2142. <b>Cylinder</b> class definition (part 1), <b>Cylinder</b> member 
  2143. function definitions (parts 2 and 3) and a driver 
  2144. program with output (parts 4 and 5). Note that class 
  2145. <b>Cylinder</b> inherits from class <b>Circle</b> with <b>public</b> 
  2146. inheritance. This means that the <b>public</b> interface to 
  2147. <b>Cylinder</b> includes the <b>Circle</b> member functions and <br>
  2148.  
  2149. </page>
  2150. <page>
  2151. <b>Point</b> member functions as well as the <b>Cylinder</b> 
  2152. member functions <b>setHeight</b>, <b>getHeight</b>, <b>area</b> 
  2153. (overridden from <b>Circle</b>) and <b>volume</b>. Note that the 
  2154. <b>Cylinder</b> constructor is required to invoke the 
  2155. constructor for its direct base class <b>Circle</b>, but not for 
  2156. its indirect base class <b>Point</b>. Each derived class 
  2157. constructor is only responsible for calling the 
  2158. constructors of that class's immediate base class (or 
  2159. classes, in the case of multiple inheritance). Also, note 
  2160. that the <b>Cylinder</b> overloaded <b>operator<<</b> function 
  2161. which is a <b>friend</b> of class <b>Cylinder</b> is able to output the 
  2162. <b>Circle</b> part of the <b>Cylinder</b> by casting the <b>Cylinder</b> 
  2163. reference <b>c</b> to a <b>Circle</b>. This results in a call to <br>
  2164.  
  2165. </page>
  2166. <page>
  2167. <b>operator<<</b> for <b>Circle</b> and outputs the <b>x</b> and <b>y</b> 
  2168. coordinates and the <b>radius</b> using the proper <b>Circle</b> 
  2169. formatting.<br>
  2170. <spacer width=16 height=1>The driver program instantiates an object of class 
  2171. <b>Cylinder</b> then uses <i>get</i> functions to obtain the 
  2172. information about the <b>Cylinder</b> object. Again, <b>main</b> is 
  2173. neither a member function nor a <b>friend</b> of class 
  2174. <b>Cylinder</b> so it cannot directly reference the <b>protected</b> 
  2175. data of class <b>Cylinder</b>. The driver program then uses 
  2176. <i>set</i> functions <b>setHeight</b>, <b>setRadius</b> and <b>setPoint</b> to 
  2177. reset the height, radius and coordinates of the cylinder. 
  2178. Finally, the driver initializes reference variable <b>pRef</b> of 
  2179. type "reference to <b>Point</b> object" (<b>Point &</b>) to <b>Cylinder</b> <br>
  2180.  
  2181. </page>
  2182. <page>
  2183. object <b>cyl</b>. It then prints <b>pRef</b> which, despite the fact 
  2184. that it is initialized with a <b>Cylinder</b> object, "thinks" it is 
  2185. a <b>Point</b> object, so the <b>Cylinder</b> object actually prints as 
  2186. a <b>Point</b> object. The driver then initializes reference 
  2187. variable <b>circleRef</b> of type "reference to <b>Circle</b> object" 
  2188. (<b>Circle &</b>) to <b>Cylinder</b> object <b>cyl</b>. The driver program 
  2189. then prints <b>circleRef</b> which, despite the fact that it is 
  2190. initialized with a <b>Cylinder</b> object, "thinks" it is a 
  2191. <b>Circle</b> object, so the <b>Cylinder</b> object actually prints as 
  2192. a <b>Circle</b> object. The area of the <b>Circle</b> is also output.<br>
  2193. <spacer width=16 height=1>This example nicely demonstrates <b>public</b> inheritance 
  2194. and defining and referencing <b>protected</b> data members. 
  2195. The reader should now be confident with the basics of <br>
  2196.  
  2197. </page>
  2198. <page>
  2199. inheritance. In the next chapter, we show how to 
  2200. program with inheritance hierarchies in a general 
  2201. manner using polymorphism. Data abstraction, 
  2202. inheritance and polymorphism are the crux of object-
  2203. oriented programming.<br>
  2204.  
  2205. </page>
  2206. </section>
  2207. <section type=Body name=Default title="9.15 Multiple Inheritance">
  2208. <page>
  2209. <font size=18 bold>9.15 Multiple Inheritance</font><hr>
  2210. So far in this chapter, we have discussed single 
  2211. inheritance in which each class is derived from exactly 
  2212. one base class. A class may be derived from more than 
  2213. one base class; such derivation is called <i>multiple 
  2214. inheritance</i>. <a href="^Practice::c:s0p0"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>Multiple inheritance means that a derived 
  2215. class inherits the members of several base classes. This 
  2216. powerful capability encourages interesting forms of 
  2217. software reuse, but can cause a variety of ambiguity 
  2218. problems.<br>
  2219. <spacer width=16 height=1>Consider the multiple inheritance example in <a href="^Code::c:s0p6"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.11</a>. 
  2220. Class <b>Base1</b> contains one <b>protected</b> data member--<b>int</b> <br>
  2221.  
  2222. </page>
  2223. <page>
  2224. <b>value</b>. <b>Base1</b> contains a constructor that sets <b>value</b> and 
  2225. <b>public</b> member function <b>getData</b> that returns <b>value</b>.<br>
  2226. <spacer width=16 height=1>Class <b>Base2</b> is similar to class <b>Base1</b> except that its 
  2227. <b>protected</b> data is <b>char letter</b>.<b> Base2</b> also has a <b>public</b> 
  2228. member function <b>getData</b>, but this function returns the 
  2229. value of <b>char letter</b>.<br>
  2230. <spacer width=16 height=1>Class <b>Derived</b> is inherited from both class <b>Base1</b> and 
  2231. class <b>Base2</b> through multiple inheritance. <b>Derived</b> has 
  2232. <b>private</b> data member <b>float</b> <b>real</b> and has <b>public</b> member 
  2233. function <b>getReal</b> that reads the value of <b>float real</b>. <br>
  2234. <spacer width=16 height=1>Note how straightforward it is to indicate multiple 
  2235. inheritance by following the colon (<b>:</b>)<b> </b> after <b>class</b> 
  2236. <b>Derived</b> with a comma-separated list of base classes. <br>
  2237.  
  2238. </page>
  2239. <page>
  2240. Note also that constructor <b>Derived</b> explicitly calls base-
  2241. class constructors for each of its base classes, <b>Base1</b> 
  2242. and <b>Base2</b>, through the member-initializer syntax. 
  2243. Again, base-class constructors are called in the order 
  2244. that the inheritance is specified, not in the order in 
  2245. which their constructors are mentioned. And if the 
  2246. base-class constructors are not explicitly called in the 
  2247. member initializer list, their default constructors will be 
  2248. called implicitly.<br>
  2249. The overloaded stream-insertion operator for <b>Derived</b> 
  2250. uses dot notation off the derived object d to print <b>value</b>, 
  2251. <b>letter</b> and <b>real</b>. This operator function is a <b>friend</b> of 
  2252. <b>Derived</b>, so <b>operator<<</b> can directly access <b>private</b> <br>
  2253.  
  2254. </page>
  2255. <page>
  2256. data member <b>real</b> of <b>Derived</b>. Also, because this 
  2257. operator is a <b>friend</b> of a derived class, it can access the 
  2258. <b>protected</b> members <b>value</b> and <b>letter</b> of <b>Base1</b> and 
  2259. <b>Base2</b>, respectively.<br>
  2260. <spacer width=16 height=1>Now let us examine the driver program in <b>main</b>. We 
  2261. create object <b>b1</b> of class <b>Base1</b> and initialize it to <b>int</b> 
  2262. value <b>10</b>. We create object <b>b2</b> of class <b>Base2</b> and 
  2263. initialize it to <b>char</b> value '<b>Z</b>'. Then, we create object <b>d</b> 
  2264. of class <b>Derived</b> and initialize it to contain <b>int</b> value <b>7</b>, 
  2265. <b>char</b> value '<b>A</b>' and <b>float</b> value <b>3.5</b>.<br>
  2266. <spacer width=16 height=1>The contents of each of the base-class objects is printed 
  2267. by calling the <b>getData</b> member function for each 
  2268. object. Even though there are two <b>getData</b> functions, <br>
  2269.  
  2270. </page>
  2271. <page>
  2272. the calls are not ambiguous because they refer directly 
  2273. to the object <b>b1</b> version of <b>getData</b> and the object <b>b2</b> 
  2274. version of <b>getData</b>. <br>
  2275. <spacer width=16 height=1>Next we print the contents of <b>Derived</b> object <b>d</b> with 
  2276. static binding. But we do have an ambiguity problem 
  2277. because this object contains two <b>getData</b> functions, one 
  2278. inherited from <b>Base1</b> and one inherited from <b>Base2</b>. 
  2279. This problem is easy to solve by using the binary scope 
  2280. resolution operator as in <b>d.Base1::getData()</b> to print 
  2281. the <b>int</b> in <b>value</b> and <b>d.Base2::getData()</b> to print the 
  2282. <b>char</b> in <b>letter</b>. The <b>float</b> value in <b>real</b> is printed without 
  2283. ambiguity with the call <b>d.getReal()</b>. Next we 
  2284. demonstrate that the <i>is a</i> relationships of single <br>
  2285.  
  2286. </page>
  2287. <page>
  2288. inheritance also apply to multiple inheritance. We 
  2289. assign the address of derived object <b>d</b> to base-class 
  2290. pointer <b>base1Ptr</b> and we print <b>int</b> <b>value</b> by invoking 
  2291. <b>Base1</b> member function <b>getData</b> off <b>base1Ptr</b>. We then 
  2292. assign the address of derived object <b>d</b> to base-class 
  2293. pointer <b>base2Ptr</b> and we print <b>char</b> <b>letter</b> by invoking 
  2294. <b>Base2</b> member function <b>getData</b> off <b>base2Ptr</b>.<br>
  2295. <spacer width=16 height=1>This example showed the mechanics of multiple 
  2296. inheritance in a simple example and introduced a 
  2297. simple ambiguity problem. <a href="^Engineer::c:s0p18"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Multiple inheritance is a 
  2298. complex topic dealt with in more detail in advanced 
  2299. C++ texts.<br>
  2300.  
  2301. </page>
  2302. <page>
  2303. <b>Select the true statement(s). </b><br>
  2304. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  2305. C++ allows a class to inherit from any number of base classes.  <br>
  2306. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Both base class constructors are called.">
  2307. When inheriting from two base classes, only one base class constructor is called.   <br>
  2308. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2309.  
  2310. </page>
  2311. </section>
  2312. <section type=Body name=Default title="9.16 Summary">
  2313. <page>
  2314. <font size=18 bold>9.16 Summary</font><hr>
  2315. <indent width=8 delay>*   One of the keys to the power of object-oriented programming is achieving software reusability through 
  2316. inheritance. </indent>
  2317. <indent width=8 delay>*   The programmer can designate that the new class is 
  2318. to inherit the data members and member functions of a 
  2319. previously defined base class. In this case, the new class 
  2320. is referred to as a derived class. </indent>
  2321. <indent width=8 delay>*   With single inheritance, a class is derived from only 
  2322. one base class. With multiple inheritance, a derived 
  2323. class inherits from multiple (possibly unrelated) base 
  2324. classes. </indent>
  2325.  
  2326. </page>
  2327. <page>
  2328. <indent width=8 delay>*   A derived class normally adds data members and 
  2329. member functions of its own, so a derived class generally has a larger definition than its base class. A derived 
  2330. class is more specific than its base class and normally 
  2331. represents fewer objects.</indent>
  2332. <indent width=8 delay>*   A derived class cannot access the <b>private</b> members 
  2333. of its base class; allowing this would violate the encapsulation of the base class. A derived class can, however, 
  2334. access the <b>public</b> and <b>protected</b> members of its base 
  2335. class. </indent>
  2336. <indent width=8 delay>*   A derived-class constructor always calls the constructor for its base class first to create and initialize the 
  2337. derived class's base-class members.</indent>
  2338.  
  2339. </page>
  2340. <page>
  2341. <indent width=8 delay>*   Destructors are called in the reverse order of constructor calls, so a derived-class destructor is called 
  2342. before its base-class destructor. </indent>
  2343. <indent width=8 delay>*   Inheritance enables software reusability which saves 
  2344. time in development and encourages the use of previously proven and debugged high-quality software. </indent>
  2345. <indent width=8 delay>*   Inheritance can be accomplished from existing class 
  2346. libraries.</indent>
  2347. <indent width=8 delay>*   Someday most software will be constructed from 
  2348. standardized reusable components exactly as most 
  2349. hardware is constructed today. </indent>
  2350. <indent width=8 delay>*  The implementor of a derived class does not need 
  2351. access to the source code of a base class, but does need </indent>
  2352.  
  2353. </page>
  2354. <page>
  2355. <indent width=8 delay>*   the interface to the base class and the base class's object 
  2356. code.</indent>
  2357. <indent width=8 delay>*   An object of a derived class can be treated as an 
  2358. object of its corresponding public base class. However, 
  2359. the reverse is not true.</indent>
  2360. <indent width=8 delay>*   A base class exists in a hierarchical relationship with 
  2361. its singly derived classes. </indent>
  2362. <indent width=8 delay>*   A class can exist by itself. When that class is used 
  2363. with the mechanism of inheritance, it becomes either a 
  2364. base class that supplies attributes and behaviors to other 
  2365. classes, or the class becomes a derived class that inherits those attributes and behaviors.</indent>
  2366. <indent width=8 delay>*  An inheritance hierarchy can be arbitrarily deep </indent>
  2367.  
  2368. </page>
  2369. <page>
  2370. <indent width=8 delay>*   within the physical limitations of a particular system.</indent>
  2371. <indent width=8 delay>*   Hierarchies are useful tools for understanding and 
  2372. managing complexity. With software becoming increasingly complex, C++ provides mechanisms for supporting hierarchical structures through inheritance and 
  2373. polymorphism.</indent>
  2374. <indent width=8 delay>*   An explicit cast can be used to convert a base-class 
  2375. pointer to a derived-class pointer. Such a pointer should 
  2376. not be dereferenced unless it actually points to an object 
  2377. of the derived class type.</indent>
  2378. <indent width=8 delay>*  <b>Protected</b> access serves as an intermediate level of 
  2379. protection between <b>public</b> access and <b>private</b> access. 
  2380. <b>Protected</b> members of a base class may be accessed by </indent>
  2381.  
  2382. </page>
  2383. <page>
  2384. <indent width=8 delay>*   members and<b> friend</b>s of the base class and by members 
  2385. and <b>friend</b>s of derived classes; no other functions can 
  2386. access the <b>protected</b> members of a base class. </indent>
  2387. <indent width=8 delay>*   <b>Protected</b> members are used to extend privileges to 
  2388. derived classes while denying those privileges to non-
  2389. class, non-<b>friend</b> functions.</indent>
  2390. <indent width=8 delay>*   Multiple inheritance is indicated by placing a colon 
  2391. (<b>:</b>) after the derived-class name and following the colon 
  2392. with a comma-separated list of base classes. Member 
  2393. initializer syntax is used in the derived-class constructor 
  2394. to call base-class constructors.</indent>
  2395. <indent width=8 delay>*  When deriving a class from a base class, the base 
  2396. class may be declared as either <b>public</b>, <b>protected</b> or </indent>
  2397.  
  2398. </page>
  2399. <page>
  2400. <indent width=8 delay>*   <b>private</b>.</indent>
  2401. <indent width=8 delay>*   When deriving a class from a <b>public</b> base class, <b>public</b> members of the base class become public members of the derived class, and <b>protected</b> members of the 
  2402. base class become <b>protected</b> members of the derived 
  2403. class. </indent>
  2404. <indent width=8 delay>*   When deriving a class from a <b>protected</b> base class, 
  2405. <b>public</b> and <b>protected</b> members of the base class 
  2406. become <b>protected</b> members of the derived class. </indent>
  2407. <indent width=8 delay>*   When deriving a class from a <b>private</b> base class, 
  2408. <b>public</b> and <b>protected</b> members of the base class 
  2409. become <b>private</b> members of the derived class.</indent>
  2410. <indent width=8 delay>*  A base class may be either a direct base class of a </indent>
  2411.  
  2412. </page>
  2413. <page>
  2414. <indent width=8 delay>*   derived class or an indirect base class of a derived class. 
  2415. A direct base class is explicitly listed where the derived 
  2416. class is declared. An indirect base class is not explicitly 
  2417. listed; rather it is inherited from several levels up the 
  2418. class hierarchy tree.</indent>
  2419. <indent width=8 delay>*   When a base-class member is inappropriate for a 
  2420. derived class, we may simply redefine that member in 
  2421. the derived class.</indent>
  2422. <indent width=8 delay>*  It is important to distinguish between "is a" relationships and "has a" relationships. In a "has a" relationship, a class object has an object of another class as a 
  2423. member. In an "is a" relationship, an object of a 
  2424. derived-class type may also be treated as an object of </indent>
  2425.  
  2426. </page>
  2427. <page>
  2428. <indent width=8 delay>*   the base-class type. "Is a" is inheritance. "Has a" is 
  2429. composition.</indent>
  2430. <indent width=8 delay>*   A derived class object can be assigned to a base class 
  2431. object. This kind of assignment makes sense because 
  2432. the derived class has members corresponding to each of 
  2433. the base class members.</indent>
  2434. <indent width=8 delay>*   A pointer to a derived-class object may be implicitly 
  2435. converted into a pointer for a base-class object.</indent>
  2436. <indent width=8 delay>*   It is possible to convert a base-class pointer to a 
  2437. derived-class pointer by using an explicit cast. The target should be a derived class object.</indent>
  2438. <indent width=8 delay>*  A base class specifies commonality. All classes 
  2439. derived from a base class inherit the capabilities of that </indent>
  2440.  
  2441. </page>
  2442. <page>
  2443. <indent width=8 delay>*   base class. In the object-oriented design process, the 
  2444. designer looks for commonality and factors it out to 
  2445. form desirable base classes. Derived classes are then 
  2446. customized beyond the capabilities inherited from the 
  2447. base class.</indent>
  2448. <indent width=8 delay>*   Reading a set of derived-class declarations can be 
  2449. confusing because not all the members of the derived 
  2450. class are present in these declarations. In particular, 
  2451. inherited members are not listed in the derived-class 
  2452. declarations, but these members are indeed present in 
  2453. the derived classes.</indent>
  2454. <indent width=8 delay>*   "Has a" relationships are examples of creating new 
  2455. classes by composition of existing classes. </indent>
  2456.  
  2457. </page>
  2458. <page>
  2459. <indent width=8 delay>*   "Knows a" relationships are examples of objects containing pointers or references to other objects so they 
  2460. can be aware of those objects. </indent>
  2461. <indent width=8 delay>*   Member object constructors are called in the order in 
  2462. which the objects are declared. In inheritance, base-
  2463. class constructors are called in the order in which inheritance is specified and before the derived-class constructor.</indent>
  2464. <indent width=8 delay>*   For a derived-class object, first the base-class constructor is called, then the derived-class constructor is 
  2465. called (which may call member object constructors). </indent>
  2466. <indent width=8 delay>*  When the derived-class object is destroyed, the 
  2467. destructors are called in the reverse order of the con</indent>
  2468.  
  2469. </page>
  2470. <page>
  2471. <indent width=8 delay>*   structors--first the derived-class destructor is called, 
  2472. then the base-class destructor is called. </indent>
  2473. <indent width=8 delay>*   A class may be derived from more than one base 
  2474. class; such derivation is called multiple inheritance. </indent>
  2475. <indent width=8 delay>*   Indicate multiple inheritance by following the colon 
  2476. (<b>:</b>) inheritance indicator with a comma-separated list of 
  2477. base classes. </indent>
  2478. <indent width=8 delay>*   The derived-class constructor calls base-class constructors for each of its base classes through the member-initializer syntax. Base-class constructors are called 
  2479. in the order in which the base classes are declared during inheritance.</indent>
  2480.  
  2481. </page>
  2482. </section>
  2483.  
  2484. <section type=Popup name=Debug title="Testing">
  2485. <page>
  2486. This chapter does not contain any Testing and Debugging tips.
  2487. </page>
  2488. </section>
  2489. <section type=Popup name=Portable title="Portability">
  2490. <page>
  2491. This chapter does not contain any Portability tips.
  2492. </page>
  2493. </section>
  2494. <section type=Popup name=AppletPopup title="Applet Examples">
  2495. <page>
  2496. This chapter does not contain any Applet Examples.
  2497. </page>
  2498. </section>
  2499. </chapter>
  2500. </html>
  2501. </html>
  2502.